Good morning,
Im trying to build a component where the data arrives in json form from a cms. I need to be able to use an object from the array as the file to include in the twig template. Something like this..
{%cards [{component: '#-card', cardOptions: {''}}, {component: '#-card_small', cardOptions: {''}}] %}
{% for card in cards %} {% include card.component with card.cardOptions only %} {% endfor %}
The problem I have it doesn't seem to parse the '#' from the json and I get the error "file.indexOf is not a function". The project is set up to use '#' as the prefix so I have no way to change that. Iv tried to concatenate in various ways but nothing works. Without the '#' symbol it parses as I expected.
Thank you for any help.
Thanks to your replace suggestion DarkBee, I fixed it by using
{% for card in cards %}
{% set card = '#' ~ card.component %}
{% include card with {card.cardOptions, card:card} only %}
{% endfor %}
Related
Learning sympony2 and I have hit a wall and have tried numerous solution but nothing seems to work, I keep getting the
Unable to find template "ScoreBoardViewerBundle.Viewer.scoreboard_keeper.html.twig" in
ScoreBoardViewerBundle:Viewer:view.html.twig at line 15.
Here is my twig snipped:
{% if score_keeper=='sk' %}
{% include 'ScoreBoardViewerBundle.Viewer.scoreboard_keeper.html.twig' with {'score_keeper' : score_keeper} %}
{% else %}
{% include 'ScoreBoardViewerBundle.Viewer.scoreboard.html.twig' %}
{% endif %}
Initially I had only the file name, like I seen on examples from this twig site: Twig site but that produced the same error. The twig files are found in the same directory.
What am I doing wrong? Also I get the same issue regardless which file I try to include. I did have some typos but they should be fixed.
Here is the Controller I was using and it was able to open both views correctly, I just noted some twig in each file was redundant so hence the changes
public function viewAction($score_keeper)
{
//returns scoreboard view for score keeper
// if($score_keeper=="sk"){
// return $this->render('ScoreBoardViewerBundle:Viewer:keeper.html.twig',array('score_keeper' => $score_keeper));
// }
//returns scorboard view for all others
return $this->render('ScoreBoardViewerBundle:Viewer:view.html.twig',array('score_keeper' => $score_keeper));
}
You have a typo in your {% include %} statements. You are supposed to use : instead of . to separate blocks in template name.
Try this:
{% if score_keeper=='sk' %}
{% include 'ScoreBoardViewerBundle:Viewer:scoreboard_keeper.html.twig' with {'score_keeper' : score_keeper} %}
{% else %}
{% include 'ScoreBoardViewerBundle:Viewer:scoreboard.html.twig' %}
{% endif %}
How do we get an element at index i in *.volt view?
I know this for loop
{% for robot in robots %}
{% if robot.type == "cyborg" %}
{{ robot.name }}
{% endif %}
{% endfor %}
but I would like to print the name of robot at index 5 only, and I don't care about other robot names.
Can I access robot at index 5 without using for loop?
Volt templates are compiled to PHP code (you can manually check it in the *.volt.php files), so you can use similar syntax to access array keys in the loop:
{% for key, robot in robots %}
{% if key == 5 %}
{{ robot.name }}
{% endif %}
{% endfor %}
or you can use regular PHP syntax to access element by index without loop:
{{ robots[5] }}
Also, looks like there is a bug with object in array case, so you can use PHP code in the Volt template to resolve your issue without loop:
<?php echo $robots[5]->name ?>
Volt is indeed based on twig but there are some functions which aren't implemented, so you should do better using the original volt documentation: http://docs.phalconphp.com/en/latest/reference/volt.html#variables
Anyways the answer is almost correct, you can access arrays via index but keep in mind that the array index begins with "0" so the correct answer is:
{{ robots[4] }}
You should be able to access a particular index of an array like this:
{{ robots[5] }}
I am writting a tutorial and need to display some twig code... Problem: I'm writting that twig code on a twig view, so I need to find some way to write it raw, without interpretation.
Currently, I am using a twig extension to do the job:
$path = $this->kernel->locateResource($path, null, true);
return file_get_contents($path);
But I'm wondering if there is better solutions.
Found it. Twig has a built-in tag to to that job : verbatim.
{% verbatim %}
<ul>
{% for item in seq %}
<li>{{ item }}</li>
{% endfor %}
</ul>
{% endverbatim %}
I wanted to do something like this:
{{ include("tpl.html")|f }}
But that doesn't seem to work, it just printed tpl.html without any filtering, then I tried:
{% filter f %}
{% include "tpl.html" %}
{% endfilter %}
And it worked. I just wonder, why can't I use shorter one? Do I misunderstand something?
Thanks in advance.
Sorry for being that long to come back :-)
The fact is that the include function writes on the template.
If you do :
{% set s = include('FuzHomeBundle:Default:test.html.twig') %}
Which is not supposed to display something, you'll get the content of the file output anyway, and the s variable will be set to null.
If you do instead :
{% filter upper %}
{% include 'FuzHomeBundle:Default:test.html.twig' %}
{% endfilter %}
or
{% filter upper %}
{{ include('FuzHomeBundle:Default:test.html.twig' }}
{% endfilter %}
The filter tag will compile some code that control output buffer.
To apply a filter on a section of code, you have to wrap it with the filter tag:
{% filter f %}
...
{% endfilter %}
What you were trying originally is to filter a variable which in twig is defined by the double parenthesis:
{{ variable name|filter }}
to read more check out the twig documentation on filters here
I want to achieve the following thing:
I have a basic template for all of my pages, named "_page_base.twig". It contains the header and footer.
Then I have a Template for different areas of the page: "topic.twig", "section.twig" and "article.twig" - each of them extending the "_page_base.twig", thats working well so far.
Now I want to write my articles. I would love to save them as .twig file as well, since I can edit the complete markup in my editor and just upload it.
Since I can't say that my article files just extend "article.twig" (multiple inheritance is not possible) I could tell the "article.twig" that it should use blocks from my different article-content twigfiles.
The problem is: "use" statements have to be hardcoded!
My current solution is to add {% use "[PLACEHOLDER]" %} into the "article.twig" and then loading the template into a string, replacing the placeholder to the correct article-content.twig and then passing the whole thing to the template engine. What a mess.
Have you guys any idea for a better solution?
You can try with include tag. Since this tag accepts a dynamic name template to include, you just only need to define a variable which contains the name of article twig template.
{# on article.twig#}
{% set articles = ['someArticle.twig', ...] %}
{% for article in articles %}
{% include article %}
{% endfor %}
{# on someArticle.twig #}
... Article text ...
In the case of you need to customize some content inside of someArticle.twig you could stage the next level: embed tag. You should define a block tag inside of someArticle.twig, this block will be the placeholder to the custom values.
{# on article.twig#}
{% set articles=['someArticle.twig', ...] %}
{% for article in articles %}
{% embed article %}
{% block inside_text_article %}
... custom text ...
{% endblock %}
{% endembed %}
{% endfor %}
{# on someArticle.twig #}
... article text ...
{% block inside_text_article %}default values{% endblock %}
... article text ...
https://github.com/fabpot/Twig/issues/17 - no dynamic namespaces
LiipThemeBundle might be a solution:
http://symfony2bundles.org/liip/LiipThemeBundle
Unless I am not understanding the question, displaying a single article only requires that it also extends __page_base.twig.
In the case that you would need to display multiple articles, you could have a template dedicated to showing a list of articles which would also extend __page_base.twig and pass the article list to this template.