Jekyll includes and YAML frontmatter - include

Is it possible to offload frontmatter to an include in some manner? My Jekyll site uses a frontmatter variable for the page title, however I have some pages that share an include due to code repetition. Putting the frontmatter for the page title in the include treats it like raw HTML. Is there a way to simulate this and set variables in an include?

According to the Jekyll docs on passing parameter variables to includes, you need to capture your front-matter as a variable first.
Their example uses capture, but you can use assign if you're just passing the variable through. Here's my tested example of how to do this:
include
<div>
{{ include.title }}
</div>
include in content
{% assign include-title = page.title %}
{% include example.html title=include-title %}

There is no front matter in includes.
But you can reach page's title from inside an include by using page.title, and any other page's variable with page.myVarName.

You can pass a parameter to an include. Try:
{% include name.html var={{page.title}} %}
Then in the include, access the parameter using include.var.
See "Pass Parameters to Includes" at https://jekyllrb.com/docs/includes/

Related

In a Hugo partial template, how do I access secondary/additional parameters?

Say I have {{ partial "li.html" $test $root.Data.Term }}.
With this I can can access the first parameter, or $test, by simply refering to . within the li.html template, but how do I access the second or additional parameter ($root.Data.Term) from within the same template?
I would suggest using the hugo dict function. It allows you to use key/value pairs to pass information. The documentation states that for your use case.
{{ partial "yourPartial" (dict "test" "yourTestData" "term" "yourTerm") }}
You can then access the values by just using {{ .test }} and {{ .term }}.
Alternatively you can use the scratch function, which is a more "global" approach.

Laravel Blade Template passing data to Vue JS component

I am having a problem passing a property using Vuejs ~1.0 to a child component from a Laravel Blade template. If I pass something in plain text it works just fine, but when I try to pass a js property, array, or object it doesn't work at all. I currently have a blade file with a custom component which looks like this:
<my-component video="#{{ stuff }}"></my-component>
If I leave out the #{{ }} the only thing that will be passed is the string stuff, and if I leave out the #, I obviously get a blade error, but if I use #{{ stuff }}, then all I get is the string {{ stuff }}. I'm obviously missing something, but can't tell where I'm going wrong. Thanks in advance.
Look like I just figured it out, it seems that I was missing the colon before video, so it should have appeared like so:
<my-component :video="stuff"></my-component>
If you are passing a variable to the component, then use:
<my-component :video= "{{ json_encode($stuff) }}" ></my-component>
Don't forget the double quotes or the result would be unpredictable for things like objects.
If you are passing model then do this:
<my-component :video="{{ $stuff->toJson() }}" inline-template></my-component>

Twig render with vars

I'm using Silex, and need to alter a Twig file based on the location its loaded from.
My first thought was checking the url, but unfortunately this returns the location of the rendered Twig location, and not the parent url as expected.
{{ path(app.request.attributes.get('_route')) }}
Another attempt was to pass some vars with the render, however I'm not certain how to do this from within Twig (and if it's possible at all).
{{ render(path('cart')) }}
Can someone possible help me with a solution?
Thanks in advance
This is indeed a problem with silex core. I have submitted a pull request that introduces a new renderRoute twig function that should fix the issue. Hopefully it will be merged soon.
Well i didn't get perfectly what are you asking, but if you want to pass variables in render method you can do this:
{% render "ProjectTesteBundle::ControllerName::actionName" with { 'variableNameToPass' : variableValue } %}
if you want get route name in twig template you can do do something like this:
{% set routeName = app.request.attributes.get('_route') %}

pyrocms files plugin variables

I want to pull a url segment variable into the pyrocms file plugin call. it would look like
{{files:listing folder="[segment(2)]"}}
or something of the sort. What is the trick for embedding
{{url:segments..}}
inside
{{files:listing folder="…}}
I am trying to setup this up for a conditional query for a photo gallery
If you take a look at the PyroCMS Tags documentation you will see this clearly documented in the "Tag Attributes" section.
You may also use the output from other tags as attribute values in your tags. For example if you wanted the url segment to default to the slug of the currently viewed page you could do this:
{{ url:segments segment="1" default=page:slug }}
Here is an example showing the proper use of quotes and braces when the tag used as the attribute value has an attribute itself.
{{ url:segments segment="1" default={foo:bar value="baz"} }}
Tip: Omit quotes and braces when using tags as attribute values. The only exception is when the tag you are using as the attribute value has its own attributes.
So you can do that easily with:
{{ files:listing folder={url:segments segment="2"} }}
Basically you don't need to pretend it's a string if it's not. You can just send the foo:bar arguments through, but it if has attributes you can "group" the attributes with the call via a single { and }.
Simple right? :)

Printing repeated Django form fields individually

I have a Django forms dilemma. When rendering a form in a template, I usually prefer to print the form fields individually, in stead of using the form.as_p annotation. However, in the form I am working on I have a set of fields that are repeated an unknown number of times. For example, if there are 3 items being edited the fields would be:
field_name_1
field_email_1
field_name_2
field_email_2
field_name_3
field_email_3
But the number of items in not known to me beforehand. To solve this I am creating the fields in a loop in the init function, no problems there. But I can't figure out a way to print the fields individually in the template, since I don't know the whole name of each field. If this were my only problem I could just use the form.as_p syntax, no biggie. But I would also like to add some html to the beginning of each set of fields, so that the result would be:
Add values 1:
field_name_1
field_email_1
Add values 2:
field_name_2
field_email_2
Add values 3:
field_name_3
field_email_3
However, I haven't found a way to insert arbitrary html into a form in Django code, similar to #markup in Drupal forms. So:
Is there a way to print the fields individually so I can put the extra html in the template? (What I am missing here is the exact syntax)
If not, how do I insert the extra html into the form?
Do you mean something like this in the template?:
{% for field in form %}
<div class="fieldWrapper">
{{ field.errors }}
{{ field.label_tag }}: {{ field }}
</div>
{% endfor %}
(from the Django documentation: https://docs.djangoproject.com/en/dev/topics/forms/?from=olddocs#looping-over-hidden-and-visible-fields )
You could render the header with something like:
Add values {{forloop.counter}}:

Resources