Accessing nested values from YAML data in a Twig template
I am trying to access nested YML data in a Twig template. My YML data is structured like this:
card_default:
card_title: 'Card Title'
card_header: 'This is the card header on a multi-part card'
card_text: "Some quick example text to build on the card title and make up the bulk of the card's content."
card_link: 'https://github.com/phase2/particle'
card_background: primary
card_image_location: top
card_footer: "This is the card footer"
text_color: uk-dark
card_body: "This is some card body text"
card_style: default
card_image:
card_more_stuff in here....
... and then I call some of the data in a Twig template like this:
{% include '@molecules/card/_card.twig' with {
card_default: {
card_title: card_title,
card_text: card_text,
card_background: 'primary',
card_link: card_link,
card_link_text: card_link_text,
card_link_class: card_link_class,
}
} only %}
But that does not seem to work. I have a feeling the way I am trying to do this is not quite right but a search didn't give me any more insight. Essentially I want to access the values within card_default
.
I can see all the data in the array if I dump with {{ dump(card_default) }}
array(14) { ["card_title"]=> string(10) "Card Title" ["card_header"]=> string(44) "This is the card header on a multi-part card" ["card_text"]=> string(94) "Some quick example text to build on the card title and make up the bulk of the card's content." ["card_link"]=> string(34) "https://github.com/phase2/particle" ["card_link_text"]=> string(9) "Read more" ["card_link_class"]=> string(27) "uk-button uk-button-default" ["card_background"]=> string(7) "primary" ["card_width"]=> int(25) ["card_image_location"]=> string(3) "top" ["card_footer"]=> string(23) "This is the card footer" ["list"]=> array(2) { ["list_flush"]=> bool(true) ["items"]=> array(3) { [0]=> array(1) { ["item_text"]=> string(15) "Cras justo odio" } [1]=> array(1) { ["item_text"]=> string(23) "Dapibus ac facilisis in" } [2]=> array(1) { ["item_text"]=> string(18) "Vestibulum at eros" } } } ["text_color"]=> string(7) "uk-dark" ["card_body"]=> string(27) "This is some card body text" ["card_style"]=> string(7) "default" }
variables twig yaml
add a comment |
I am trying to access nested YML data in a Twig template. My YML data is structured like this:
card_default:
card_title: 'Card Title'
card_header: 'This is the card header on a multi-part card'
card_text: "Some quick example text to build on the card title and make up the bulk of the card's content."
card_link: 'https://github.com/phase2/particle'
card_background: primary
card_image_location: top
card_footer: "This is the card footer"
text_color: uk-dark
card_body: "This is some card body text"
card_style: default
card_image:
card_more_stuff in here....
... and then I call some of the data in a Twig template like this:
{% include '@molecules/card/_card.twig' with {
card_default: {
card_title: card_title,
card_text: card_text,
card_background: 'primary',
card_link: card_link,
card_link_text: card_link_text,
card_link_class: card_link_class,
}
} only %}
But that does not seem to work. I have a feeling the way I am trying to do this is not quite right but a search didn't give me any more insight. Essentially I want to access the values within card_default
.
I can see all the data in the array if I dump with {{ dump(card_default) }}
array(14) { ["card_title"]=> string(10) "Card Title" ["card_header"]=> string(44) "This is the card header on a multi-part card" ["card_text"]=> string(94) "Some quick example text to build on the card title and make up the bulk of the card's content." ["card_link"]=> string(34) "https://github.com/phase2/particle" ["card_link_text"]=> string(9) "Read more" ["card_link_class"]=> string(27) "uk-button uk-button-default" ["card_background"]=> string(7) "primary" ["card_width"]=> int(25) ["card_image_location"]=> string(3) "top" ["card_footer"]=> string(23) "This is the card footer" ["list"]=> array(2) { ["list_flush"]=> bool(true) ["items"]=> array(3) { [0]=> array(1) { ["item_text"]=> string(15) "Cras justo odio" } [1]=> array(1) { ["item_text"]=> string(23) "Dapibus ac facilisis in" } [2]=> array(1) { ["item_text"]=> string(18) "Vestibulum at eros" } } } ["text_color"]=> string(7) "uk-dark" ["card_body"]=> string(27) "This is some card body text" ["card_style"]=> string(7) "default" }
variables twig yaml
add a comment |
I am trying to access nested YML data in a Twig template. My YML data is structured like this:
card_default:
card_title: 'Card Title'
card_header: 'This is the card header on a multi-part card'
card_text: "Some quick example text to build on the card title and make up the bulk of the card's content."
card_link: 'https://github.com/phase2/particle'
card_background: primary
card_image_location: top
card_footer: "This is the card footer"
text_color: uk-dark
card_body: "This is some card body text"
card_style: default
card_image:
card_more_stuff in here....
... and then I call some of the data in a Twig template like this:
{% include '@molecules/card/_card.twig' with {
card_default: {
card_title: card_title,
card_text: card_text,
card_background: 'primary',
card_link: card_link,
card_link_text: card_link_text,
card_link_class: card_link_class,
}
} only %}
But that does not seem to work. I have a feeling the way I am trying to do this is not quite right but a search didn't give me any more insight. Essentially I want to access the values within card_default
.
I can see all the data in the array if I dump with {{ dump(card_default) }}
array(14) { ["card_title"]=> string(10) "Card Title" ["card_header"]=> string(44) "This is the card header on a multi-part card" ["card_text"]=> string(94) "Some quick example text to build on the card title and make up the bulk of the card's content." ["card_link"]=> string(34) "https://github.com/phase2/particle" ["card_link_text"]=> string(9) "Read more" ["card_link_class"]=> string(27) "uk-button uk-button-default" ["card_background"]=> string(7) "primary" ["card_width"]=> int(25) ["card_image_location"]=> string(3) "top" ["card_footer"]=> string(23) "This is the card footer" ["list"]=> array(2) { ["list_flush"]=> bool(true) ["items"]=> array(3) { [0]=> array(1) { ["item_text"]=> string(15) "Cras justo odio" } [1]=> array(1) { ["item_text"]=> string(23) "Dapibus ac facilisis in" } [2]=> array(1) { ["item_text"]=> string(18) "Vestibulum at eros" } } } ["text_color"]=> string(7) "uk-dark" ["card_body"]=> string(27) "This is some card body text" ["card_style"]=> string(7) "default" }
variables twig yaml
I am trying to access nested YML data in a Twig template. My YML data is structured like this:
card_default:
card_title: 'Card Title'
card_header: 'This is the card header on a multi-part card'
card_text: "Some quick example text to build on the card title and make up the bulk of the card's content."
card_link: 'https://github.com/phase2/particle'
card_background: primary
card_image_location: top
card_footer: "This is the card footer"
text_color: uk-dark
card_body: "This is some card body text"
card_style: default
card_image:
card_more_stuff in here....
... and then I call some of the data in a Twig template like this:
{% include '@molecules/card/_card.twig' with {
card_default: {
card_title: card_title,
card_text: card_text,
card_background: 'primary',
card_link: card_link,
card_link_text: card_link_text,
card_link_class: card_link_class,
}
} only %}
But that does not seem to work. I have a feeling the way I am trying to do this is not quite right but a search didn't give me any more insight. Essentially I want to access the values within card_default
.
I can see all the data in the array if I dump with {{ dump(card_default) }}
array(14) { ["card_title"]=> string(10) "Card Title" ["card_header"]=> string(44) "This is the card header on a multi-part card" ["card_text"]=> string(94) "Some quick example text to build on the card title and make up the bulk of the card's content." ["card_link"]=> string(34) "https://github.com/phase2/particle" ["card_link_text"]=> string(9) "Read more" ["card_link_class"]=> string(27) "uk-button uk-button-default" ["card_background"]=> string(7) "primary" ["card_width"]=> int(25) ["card_image_location"]=> string(3) "top" ["card_footer"]=> string(23) "This is the card footer" ["list"]=> array(2) { ["list_flush"]=> bool(true) ["items"]=> array(3) { [0]=> array(1) { ["item_text"]=> string(15) "Cras justo odio" } [1]=> array(1) { ["item_text"]=> string(23) "Dapibus ac facilisis in" } [2]=> array(1) { ["item_text"]=> string(18) "Vestibulum at eros" } } } ["text_color"]=> string(7) "uk-dark" ["card_body"]=> string(27) "This is some card body text" ["card_style"]=> string(7) "default" }
variables twig yaml
variables twig yaml
edited Nov 14 '18 at 0:26
Danny Englander
asked Nov 13 '18 at 23:19
Danny EnglanderDanny Englander
99942035
99942035
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The data is in the variable card_default
, so it should be e.g. card_default.card_title
but instead of creating a whole new object you just could do this in 2 ways:
YAML
foo:
bar: 'foobar'
number: 42
main.twig
{% include "full.twig" with { 'foo' : foo } only %}
{% include "slim.twig" with foo only %}
full.twig
{{ foo.bar }}
slim.twig
{{ number }}
Although that would work, it does not keep my base template DRY. I figured this out, all I needed to do was to call it properly, I'll post an answer below.
– Danny Englander
Nov 14 '18 at 14:59
Well yeah, that is what I stated in my first sentence actually :)
– DarkBee
Nov 14 '18 at 15:16
add a comment |
I figured this out, I just needed to map the nested items properly like so:
{% include '@molecules/card/_card.twig' with {
card_title: card_default.card_title,
card_text: card_default.card_text,
card_background: 'primary',
card_link: card_default.card_link,
card_link_text: card_default.card_link_text,
card_link_class: card_default.card_link_class,
} %}
In the above code, card_default
is mapped in the variable portion of the array, i.e., after the colon. card_link: card_default.card_link,
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290937%2faccessing-nested-values-from-yaml-data-in-a-twig-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The data is in the variable card_default
, so it should be e.g. card_default.card_title
but instead of creating a whole new object you just could do this in 2 ways:
YAML
foo:
bar: 'foobar'
number: 42
main.twig
{% include "full.twig" with { 'foo' : foo } only %}
{% include "slim.twig" with foo only %}
full.twig
{{ foo.bar }}
slim.twig
{{ number }}
Although that would work, it does not keep my base template DRY. I figured this out, all I needed to do was to call it properly, I'll post an answer below.
– Danny Englander
Nov 14 '18 at 14:59
Well yeah, that is what I stated in my first sentence actually :)
– DarkBee
Nov 14 '18 at 15:16
add a comment |
The data is in the variable card_default
, so it should be e.g. card_default.card_title
but instead of creating a whole new object you just could do this in 2 ways:
YAML
foo:
bar: 'foobar'
number: 42
main.twig
{% include "full.twig" with { 'foo' : foo } only %}
{% include "slim.twig" with foo only %}
full.twig
{{ foo.bar }}
slim.twig
{{ number }}
Although that would work, it does not keep my base template DRY. I figured this out, all I needed to do was to call it properly, I'll post an answer below.
– Danny Englander
Nov 14 '18 at 14:59
Well yeah, that is what I stated in my first sentence actually :)
– DarkBee
Nov 14 '18 at 15:16
add a comment |
The data is in the variable card_default
, so it should be e.g. card_default.card_title
but instead of creating a whole new object you just could do this in 2 ways:
YAML
foo:
bar: 'foobar'
number: 42
main.twig
{% include "full.twig" with { 'foo' : foo } only %}
{% include "slim.twig" with foo only %}
full.twig
{{ foo.bar }}
slim.twig
{{ number }}
The data is in the variable card_default
, so it should be e.g. card_default.card_title
but instead of creating a whole new object you just could do this in 2 ways:
YAML
foo:
bar: 'foobar'
number: 42
main.twig
{% include "full.twig" with { 'foo' : foo } only %}
{% include "slim.twig" with foo only %}
full.twig
{{ foo.bar }}
slim.twig
{{ number }}
answered Nov 14 '18 at 6:46
DarkBeeDarkBee
8,77242842
8,77242842
Although that would work, it does not keep my base template DRY. I figured this out, all I needed to do was to call it properly, I'll post an answer below.
– Danny Englander
Nov 14 '18 at 14:59
Well yeah, that is what I stated in my first sentence actually :)
– DarkBee
Nov 14 '18 at 15:16
add a comment |
Although that would work, it does not keep my base template DRY. I figured this out, all I needed to do was to call it properly, I'll post an answer below.
– Danny Englander
Nov 14 '18 at 14:59
Well yeah, that is what I stated in my first sentence actually :)
– DarkBee
Nov 14 '18 at 15:16
Although that would work, it does not keep my base template DRY. I figured this out, all I needed to do was to call it properly, I'll post an answer below.
– Danny Englander
Nov 14 '18 at 14:59
Although that would work, it does not keep my base template DRY. I figured this out, all I needed to do was to call it properly, I'll post an answer below.
– Danny Englander
Nov 14 '18 at 14:59
Well yeah, that is what I stated in my first sentence actually :)
– DarkBee
Nov 14 '18 at 15:16
Well yeah, that is what I stated in my first sentence actually :)
– DarkBee
Nov 14 '18 at 15:16
add a comment |
I figured this out, I just needed to map the nested items properly like so:
{% include '@molecules/card/_card.twig' with {
card_title: card_default.card_title,
card_text: card_default.card_text,
card_background: 'primary',
card_link: card_default.card_link,
card_link_text: card_default.card_link_text,
card_link_class: card_default.card_link_class,
} %}
In the above code, card_default
is mapped in the variable portion of the array, i.e., after the colon. card_link: card_default.card_link,
add a comment |
I figured this out, I just needed to map the nested items properly like so:
{% include '@molecules/card/_card.twig' with {
card_title: card_default.card_title,
card_text: card_default.card_text,
card_background: 'primary',
card_link: card_default.card_link,
card_link_text: card_default.card_link_text,
card_link_class: card_default.card_link_class,
} %}
In the above code, card_default
is mapped in the variable portion of the array, i.e., after the colon. card_link: card_default.card_link,
add a comment |
I figured this out, I just needed to map the nested items properly like so:
{% include '@molecules/card/_card.twig' with {
card_title: card_default.card_title,
card_text: card_default.card_text,
card_background: 'primary',
card_link: card_default.card_link,
card_link_text: card_default.card_link_text,
card_link_class: card_default.card_link_class,
} %}
In the above code, card_default
is mapped in the variable portion of the array, i.e., after the colon. card_link: card_default.card_link,
I figured this out, I just needed to map the nested items properly like so:
{% include '@molecules/card/_card.twig' with {
card_title: card_default.card_title,
card_text: card_default.card_text,
card_background: 'primary',
card_link: card_default.card_link,
card_link_text: card_default.card_link_text,
card_link_class: card_default.card_link_class,
} %}
In the above code, card_default
is mapped in the variable portion of the array, i.e., after the colon. card_link: card_default.card_link,
answered Nov 14 '18 at 15:03
Danny EnglanderDanny Englander
99942035
99942035
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53290937%2faccessing-nested-values-from-yaml-data-in-a-twig-template%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown