I have defined a variable in a page:
{% set service = builderDetails.record %}
and I want to pass this variable to a component:
{% component 'Variations' service=service %}
but this method does not work. How can I do that?
I am guessing that you need to pass builderDetails.record to your component 'Variations'
and then you want to access that variables inside component 'Variations''s default.htm and show some details about it.
for that you need to utilize component's onRender method
inside your component 'Variations'
public function onRender()
{
$this->page['record'] = $this->page->components['builderDetails']->record;
}
$this->page->components is holding all the components available in page as array.
builderDetails is alias of the component Record Details(Builder Plugin) which is added in to page.
now inside your default.htm
you can access record variable and use it as you desire
{{ record.name }}
we assume here that your record has attribute name
if you need anything please comment.
In a Twig .htm partial
<div class="sidebar-widget">
{% component "postBlock" score=55 name="Johan" %}
<div class="nonsense meta">
{% partial 'nonsense' score=75 name="Marga" %}
</div>
</div>
In the nonsense twig that is a child of the previous .htm partial
<div class="sidebar-element">
<div>My name is: {{ name|default('John Doe') }}</div>
<div>My score is: {{ score|default('No score') }}</div>
</div>
In the component postBlock, onRender function
function onRender() {
$score = $this->property('score'); // 55
$name = $this->property('name'); // Johan
}
To pass from a parent twig to a child twig, you don't have to use the onRender function
in the component, only if you want to modify things and pass is from there to the twig.
You dont have to assign it to page. You can access it via:
__SELF__.property('property_name')
After definin property in component:
{% component 'Variations' service=service %}
In your components default.htm
{{__SELF__.property('service')}}
Related
does anyone know now to create a custom view type for ez platform? The default 3 have been exhausted and we need a new one for 'link'
Alternatively, does anyone know how to use the render( controller( with a custom template as this would also resolve out block right now.
Basically, we have a multi-relational field in a content object used and we need to print links to all the related contentIds, path works great but we cannot find a way to extract the name of the content object for the link without doing some fairly funky tpl logic of passing in params.
EG: As a hack for now we can pass in "embed_type" as a custom param with the render(controller("ez_content:viewAction" to pull in an alternate view for the content object for a specific content type and view type.
{% if embed_type is defined %}
{% include "embed/#{embed_type}.html.twig" %}
{% else %}
<h1>{{ ez_field_value( content, 'name') }}</h1>
{% endif %}
However, this is very ugly and all we really want to do is use 1 template for all content types, so all we need to do is loop through the relational field and print links (as the only thing available in the content field: "destination ids"). I am sure there used to be this option in the docs but i cannot find it anymore eg:
{% set links = ez_field_value( footer, "first_links_row" ).destinationContentIds%}
{% for id in links %}
{{ render(controller("ez_content:viewAction", {"contentId": id, "template": "link.html.twig"})) }}
{% endfor %}
Where the link.html.twig would simple print the link:
<a href="{{ path( "ez_urlalias", {"contentId": id} ) }}">
{{ ez_field_value( content, "name" ) }}
</a>
If using a custom tpl is not possible with the render (controller ( helper then a new custom view type would also fix this issue, but i cannot find documentation for either.
You can create a twig function that would do that. We have something like this:
Definition:
new Twig_SimpleFunction(
'content_name',
array($this, 'getContentName')
),
Implementation:
public function getContentName($content, $forcedLanguage = null)
{
if (!$content instanceof Content && !$content instanceof ContentInfo) {
$contentInfo = $this->repository->getContentService()->loadContentInfo($content);
} elseif ($content instanceof Content) {
$contentInfo = $content->contentInfo;
} else {
$contentInfo = $content;
}
return $this->translationHelper->getTranslatedContentNameByContentInfo($contentInfo, $forcedLanguage);
}
which enables you to provide either content id, content info or content itself, and it returns translated content name
There are some way to extract values from extra-fields of static pages into some component?
I need that to implement AJAX handlers...
I can load the static page content but, I need to get properties that was setted by user...
I need some example of code... I can load the static page content as a Partial
#noobhacks :)
public function onOpenHome()
{
$this->page['categories'] = Category::all();
return [
//'main' => $this->renderPartial('home'),
'main' => $this->renderPartial('../content/static-pages/index'),
'.home_categories' => $this->renderPartial('work_list_categories_post')
];
}
or as content:
[viewBag]
==
<main class="home_container" __color="{{ color }}">
<section class="box">
{% content '/static-pages/index.htm' %} <!-- <<<<------ -->
{{ content |raw }}
</section>
</main>
But I have no ideia how to access to property of colorpicker color
Component objects are available using their name (or alias). This means the value of "color" should be available via {{ viewBag.color }}.
I'm wondering how I would go about using custom variables in base.html.twig in a symphony application.
I know I can use {{ app.whatever }} but how would I use {{ myvariable }} or {{ myentity.row }} if I wanted to?
Thanks
As a variable is rendered with a twig template, you can use this variable in both parent and child templates.
In other words, if you have the following base template:
// base.html.twig
<html>
<body>
{{ block body }}
{{ endblock }}
</body>
</html>
The following child template:
// child.html.twig
{% extends 'base.html.twig' %}
{% block body %}
// content
{% endblock %}
And the following controller action:
public function renderVariableAction()
{
return $this->render('child.html.twig', [
'hello' => 'Hello world',
]);
}
You can use {{ hello }} in both base.html.twig and child.html.twig.
EDIT
For a global variable:
// app/config/config.yml
# ...
twig:
# ...
globals:
your_custom_var: "your_value"
You can't define a variable that is always assigned to a specific template, the variable must be rendered with it dynamically.
Note You can define global variables dynamically like this:
$this->get('twig')->addGlobal('entity', $entity);
So you can easily inject the same variable on kernel.response using an EventListener.
See global variables in templates.
I have a function in my Symfony controller which returns the following:
test+testString1+test2
TestController.php
public function getResultAction($fileName) {
$string1="test";
$string2="testString1";
$string3="test2";
$response = $string1."+".$string2."+".$string3;
return new Response($response);
}
In my twig I've rendered the function in my controller:
test.html.twig
{% set test %}
{{ render(controller('TestBundle:Test:getResult')) }}|split('+', 4)
{% endset %}
{{ test[0] }}
I'm using twig split filter so that I can display test, testString1 and test2 individually. But then whenever I attempt to display test[0], I receive the following error:
Impossible to access a key "0" on an object of class "Twig_Markup" that does not implement ArrayAccess interface in TestBundle:Test:test.html.twig
What's wrong with what I'm doing? I hope you could help me with this. Thanks
You miss the split filter inside the double brace as follow:
{% set test %}
{{ render(controller('TestBundle:Test:getResult')) |split('+', 4) }}
{% endset %}
Hovenever seems same problem with the set tag. From the doc:
The set tag can also be used to 'capture' chunks of text
Try with:
{%
set test=render(controller('TestBundle:Test:getResult'))|split('+', 4)
%}
Hope this help
i can't figure out, how to render a macro in Symfony 2 controller. This is how i can render a twig template
$this
->get("twig")
->render("AcmeBundle:Product:table.html.twig", array(
"product" => $product
))
;
So i searching for something similar, but for rendering a twig macro. Thx for any suggestions!
Twig macro's are something inside a template. They are run whenever you render a template executing the macro.
Just create another "wraper" template, which would have only that macro in it. Something like
macro.html.twig file
{% macro sample(item) %}
{# some code here #}
{% endmacro sample #}
sample_macro_wrapper.html.twig
{% from 'macro.html.twig' import sample %}
{{ sample(item) }}
controller.php
public function someAction()
{
// ...........
$renderedMacro = $this->get('twig')
->render('sample_macro_wrapper.html.twig', ['item' => $item]);
}