Chapters
33 Chapters
|
3:02:08
|
Login to bookmark this video
-
Course Code
Subscribe to download the code!Compatible PHP versions: ^7.2.0
Subscribe to download the code!Compatible PHP versions: ^7.2.0
-
This Video
Subscribe to download the video!
Subscribe to download the video!
-
Subtitles
Subscribe to download the subtitles!
Subscribe to download the subtitles!
-
Course Script
Subscribe to download the script!
Subscribe to download the script!
25.
JavaScript Templating
Scroll down to the script below, click on any sentence (including terminal blocks) to jump to that spot in the video!
Subscribe to jump to this part in the video!
Keep on Learning!
If you liked what you've learned so far, dive in! Subscribe to get access to this tutorial plus video, code and script downloads.
This tutorial uses an older version of Symfony... but since it's a JavaScript tutorial, the concepts are still ? valid!
What PHP libraries does this tutorial use?
// composer.json
{
"require": {
"php": "^7.2.0",
"symfony/symfony": "3.1.*", // v3.1.10
"twig/twig": "2.10.*", // v2.10.0
"doctrine/orm": "^2.5", // v2.7.1
"doctrine/doctrine-bundle": "^1.6", // 1.10.3
"doctrine/doctrine-cache-bundle": "^1.2", // 1.3.2
"symfony/swiftmailer-bundle": "^2.3", // v2.4.0
"symfony/monolog-bundle": "^2.8", // 2.12.0
"symfony/polyfill-apcu": "^1.0", // v1.2.0
"sensio/distribution-bundle": "^5.0", // v5.0.22
"sensio/framework-extra-bundle": "^3.0.2", // v3.0.16
"incenteev/composer-parameter-handler": "^2.0", // v2.1.2
"friendsofsymfony/user-bundle": "~2.0@dev", // dev-master
"doctrine/doctrine-fixtures-bundle": "~2.3", // v2.4.1
"doctrine/doctrine-migrations-bundle": "^1.2", // v1.2.1
"composer/package-versions-deprecated": "^1.11", // 1.11.99
"friendsofsymfony/jsrouting-bundle": "^1.6" // 1.6.0
},
"require-dev": {
"sensio/generator-bundle": "^3.0", // v3.1.1
"symfony/phpunit-bridge": "^3.0" // v3.1.6
}
}
17 Comments
I have this error when I send the request
`
VM40780:8 Uncaught ReferenceError: trans is not defined
I just find my error: I forgot the filter trans near itemLabel ...
Is there a reason why underscorejs was just used as a global library, rather than passing it into the function like was done with `window`, `jQuery` and, eventually, `Routing` and `swal`?
Hey tomchkkk!
No reason at all :). Actually, good eye - that was a mistake! With the self-executing blocks, you're trying to be "responsible" by passing what you need into the function. But, unfortunately, all of these variables - jQuery, Routing, _ - *are* truly global variables... which you're basically trying to convert to local variables while inside the function. But, you can see how easy it is to forget a detail, and suddenly start using the global variable.
In our Webpack tutorial (https://knpuniversity.com/s... or Webpack Encore tutorial (https://knpuniversity.com/s... - Encore is just a "shortcut" around Webpack) we talk about "module loading", which is an improvement on this method, and a situation where I would *not* have been able to make this mistake.
Cheers!
Hey guys,
So, if your front end communicates with the back end via an API then it seems to me that all templates for an app would need to use Underscore rather than Twig, is that right?
Hey Shaun T.
You are correct, Twig is used for backend rendering, but you don't have to use "Underscore" obligatory, you may want to use ReactJS or any other template engine for frontend (psst, utilize ReactJS)
Cheers!
Hello, about this: this.$wrapper.find('tbody').append($.parseHTML(html));
When I use
this.$wrapper.find('tbody').append(html); it works exactly the same way, so why do we need to parse html here?
Hey Thao L.
By using *$.parseHtml()* method you are ensuring that all the HTML injected it's been rendered correctly by the browser
You can find more information about it here:
https://api.jquery.com/jque...
Have a nice day!
Hi, I am getting a weird error over here:
```javascript
VM1229:6 Uncaught ReferenceError: repLog is not defined
at eval (eval at m.template (underscore.js:1454), <anonymous>:6:9)
at c (underscore.js:1461)
at window.RepLogApp._addRow (RepLogApp.js:127)
at Object.success (RepLogApp.js:82)
at i (jquery-3.1.1.min.js:2)
at Object.fireWith [as resolveWith] (jquery-3.1.1.min.js:2)
at A (jquery-3.1.1.min.js:4)
at XMLHttpRequest.<anonymous> (jquery-3.1.1.min.js:4)
(anonymous) @ VM1229:6
c @ underscore.js:1461
_addRow @ RepLogApp.js:127
success @ RepLogApp.js:82
i @ jquery-3.1.1.min.js:2
fireWith @ jquery-3.1.1.min.js:2
A @ jquery-3.1.1.min.js:4
(anonymous) @ jquery-3.1.1.min.js:4
```
The row doesn't get updated...
Hey Daniel!
I believe this is basically a "Variable not found" error when it renders the template. In other words, the template needs a
repLogvariable, but there isn't one available. If you look at the last code block on the page, it shows the finished template (the code inside of thejs-rep-log-row-templatescript tag. You shouldn't actually have arepLogvariable in your template. Does your template have this variable? If it does have the variable, kill it! Make your's look like mine :). If not, post some more code - I want to see exactly what's going on.Cheers!
Thanks Ryan! I missed delete the Twig related variables on the template.
Cheers
Hey! I never worked with underscore-js. Why don't we have to write repLog.itemLabel, repLog.reps, ... in our template (as in twig files?)? Thx!
Hi Max!
Ah, it's just due to a subtle difference in how we're passing the variables to the Twig template versus the Underscore template - it's not due to any difference in how they work. Let me know show you :)
But in Underscore, we're pass the repLog itself as the array of arguments, so its keys become the variables.
Does that make sense? We could almost do the same thing in Twig:
Cheers!
Hey Ryan!
Cool! I never thought about using an associative array as twig parameter.
But if I wanted to render two different JS-Objects I would need to add the object.(.xy...) prefix, right?
Best
Hey Max,
If you have to pass 2 or more objects - then simply pass object of objects:
Cheers!
Hi everyone, is it possible to get the loggedin user in an underscore template?
Thanks
Hey Julien,
If you're in Twig template, you can get the current user id with {{ app.user.id }} and put it on any data property. I think it makes sense to set it on body tag, but it's up to you, for example:
So if there's a logged in user or no - you'll know it in JS by parsing "user-id" data attribute with jQuery like "$('body').data('user-id')" or manually.
Does it makes sense for you?
Cheers!
"Houston: no signs of life"
Start the conversation!