Passing string variables through include in twig and slim framework - twig

I am using Slim Framework and Twig.
I want to apply the DRY by using partials. I have a form that is reused in several views with different variables such as titles and route (url) names.
I am struggling on how to make it work on url names.
For example, there is this link using the ´urlFor´ helper with parameter as follows inside a view:
The link
So that is the link I want to pass to the partial template since, it is different in each view, I want to use the partial form. I have tried several approaches but it does not work. I don't know how to pass this string containing ' inside.
For example, I have tried this partial call inside the parent view like this:
{% include 'partials/partial.php' with {'theUrl': "urlFor('route.name', {parameter: value})"} %}
And inside the partial like this:
Show more
It does not work because in the browser's url I see the following:
http://myproject.dev/pages/urlFor('route.name',%20%7Bparameter:%201%7D)
It looks like it is not being escaped properly. Any ideas how to fix this when passing route names with urlFor()?

Without the greatest possibility for testing i think i found the problem. You are simply adding the function inside a string and that does not give the parser an oportunity to resolve the method, because it's basicly just a string.
{% include 'partials/partial.php' with {'theUrl': "urlFor('route.name',{parameter: value})"} %}
Should be
{% include 'partials/partial.php' with {'theUrl': urlFor('route.name',{parameter: value})} %}

Related

Calling method in Twig, how to avoid the return showing as output

I am calling a method in a pre-existing object from inside a Twig template, after properly authorizing it in policy:
{{ thisEmail.AddCC('me#example.com') }}
My problem is that the object method, which I use to produce a side-effect, not to return any text, returns true, and that leaves a visible "1" in my template output.
How to remove this output? I have this one working:
{{ thisEmail.AddCC('me#example.com')|trim('1') }}
But I feel it's not entirely elegant, and it can only remove that specific result. I would prefer to write something like |nul or |drop and have the filter drop everything, regardless of how much or what kind of stuff it receives as input.
I know how to do it by writing my own Twig extension filters, but my question here is: is there a nice way of achieving this with the core Twig language, un-extended?
If you want a twig statement not to output anything you can use the do tag, e.g.
{% do thisEmail.AddCC('me#example.com') %}
source

I cannot modify twig file

Has anyone else encountered this problem?
I cannot modify files of this type:
{% include 'mobshop/template/common/icons/wishlist.twig' %}
The file "wishlist.twig" is modified in the log but the changes do not appear live.
Do you have any suggestions?
Short answer: you can't modify with OCMod twig files that are added via include method inside the twig template.
How twig include works?
include is twig method that allows you to add partials into your theme which is a cool feature. when twig engin runs, it compiles the template by following the link and adding the html part into the final html output string.
How OCMod works?
OCmod is basically a function that takes in the path of the file (in our case the twig template file path) and after parsing the string modifies it and saves to the OCMod cache.
Then, when OpenCart asks for that file, the OCMod engine tries to first return the cached file, and if that is not available, then the original file.
so all files that are wrapped in modification('') method have this support.
The reason why twig include is not supported by OCMod
From the logic above we can see that the OCMod modification method simply never sees the twig partial file path from the Include method. It is jsut beyond its scope.
The modification method sees only a string {% include 'path-to-partial-file' %} and that is it. it never dives into that path and never tries to create a OCMod cache off of that file.
Conclusion.
You should not use "include" in your themes at all. Its just bad practice in OpenCart themes. Although personally I love this feature of Twig, I am also forced to avoid it.
The only way you should add partials in OpenCart is via the Controller ($this->load->controller('...')) attaching it to the $data field and then displaying it in the template.
And if you still MUST have this feature
PHP Twig engine is a powerful tool and you can extend it to your needs. You can still add an extension that can make the include method to work with OCmod, although I have never added that feature.
Here is a twig extension https://github.com/Dreamvention/2_d_twig_manager/blob/master/system/library/template/Twig/Extension/DTwigManager.php that you can use as an example and modify to your needs.
Enjoy!

Ckeditor - using twig code within FOSCKEditor wysiwyg

I wanted to know if it was possible to put twig code in ckeditor and that it interprets correctly the code in order to generate the HTML code.
I've already seen some configurations (using "protectedSource") that allow to put twig code within ckeditor but when I do that, the twig code is still interpreted as a string.
My goal here is to create some twig functions that I could use inside CKEditor.
Example :
Let's say that the "my_complex_table_function" function return a complex table, i would like to be able to put
{{ my_complex_table_function }}
in CKEditor and that it returns the table in the front page.
Is that possible ?
Thanks guys

page.collection() is empty despite having child pages

I'm using Masonry theme and trying to show all posts in a collection. Masonry does this normally and I had it working previously, but now page.collection is empty despite having pages. I've made sure that the children are routable and visible. I even reinstalled the theme, to no avail. Any ideas? At this point I haven't actually modified any code that would affect that page logic relating to the collections.
At the top of the file is:
{% set collection = page.collection() %}
I used
{{dump(collection)}}
But nothing shows up. What gives?
I changed page.collection to page.children instead. It accomplishes the same thing without having to add extra header content on my page. Thanks to HungTran for pointing out the correct way though.
If collection() is indeed a method, then you would use this in Twig instead:
{% set collection = page.collection %}
Can you try that. I'm not certain it will work, but let us know the result.

Append code to a block from included partial

I have a js block in my template that collects all javascripts on the bottom of the page. I usually extend base template and append JSs using parent() Twig function to keep child scripts below parent ones. It works well but now I would like to include a partial that would also append scripts to the js block. Something like this:
http://twigfiddle.com/3w8g0o
I want following order:
main content 1
partial content
main content 2
base js
main js
partial js
If I use use instead of include, then parent() returns js block from partial.twig instead of base.twig.
The only solution I found would be to
Add use of the partial above the main content block
Replace include with block('partial_content')
Append js from main.twig with block('partial_js')
http://twigfiddle.com/3w8g0o/2
which means there are 3 things to remember when including new partial instead of 1 which will probably lead to forgetting something some day. Is there an easier (one line from main.twig preferably, partial.twig can be more complex) way to achieve my desired order?

Resources