I have an "included" template with several parameters. The contents of the parameters get a bit muddled if I cram them all into a single line, so I would prefer something like this:
{% include product_details
weight= "5.8lbs (2.6 kg)"
width= "22" (56cm)"
length= "49" (125cm)"
thickness= "1¼" (3cm)"
case= "MT51413"
%}
However, this gives me the following error when generating the site:
error: Tag '{%' was not properly terminated with regexp: /\%}/.
Is there any way to spread a Liquid include over several lines?
Sorry you can't, the liquid parser expects the whole statement in one line, this is true for all statements not only for include, try to split an if statement and you receive the same error
Related
The following Go template processes without error:
{{block "A" "hello"}}{{end}}
{{define "A"}}{{.}}{{end}}
The output is "hello", as I would expect from reading the documentation. In contrast, the following template does not parse:
{{block "A" "hello"}}A{{end}}
{{define "A"}}{{.}}{{end}}
Here I get the error message
template: multiple definition of template "A"
Why does the second template give an error while the first does not? Is this difference intended?
Complete code is here: https://play.golang.org/p/CNAqllVLjB
The answer lies in the doc of html/template.Parse():
[...] It is an error if a resulting template is non-empty (contains content other than template definitions) and would replace a non-empty template with the same name. (In multiple calls to Parse with the same receiver template, only one call can contain text other than space, comments, and template definitions.)
Your first template works because {{block "A"}} defines an empty template, and so it is allowed to be re-defined.
Your second template fails because {{block "A"}} defines a non-empty template and yet you try to re-define it with a non-empty template.
One thing to note here: I quoted the doc from the html/template package, which should be "identical" to text/template. It is most of the time, but text/template.Parse() is different, and leaves out this important detail, but they work the same way. This is a documentation inconsistency, filed an issue which can be tracked here: issue #17360.
Does Jekyll provide a way to include a post inside another post? I know that sounds a bit goofy, but I'm using it for a cooking/recipe site. Some of my recipes are made up of components, or other, smaller recipes.
I'm looking for a way to include a handful of posts inside another post, complete with template and all. Is this possible?
I did a ton of googling, and found I was only able to include into a page, not post, and it didn't use the default liquid template, just plain markdown.
Ideas?
I used Jekyll's Collections in my website to solve a problem similar to yours.
Collections are still an experimental feature in Jekyll, and the solution I used is pretty hacky by itself, but if you don't go too crazy with it it's manageable enough.
For a more detailed and friendly intro to collections, here's a great post about getting started with them.
One thing we need to have in mind is that we should get away from posts for cases like this.
As mentioned by Ben Balter in the link above, “If people are using blog posts for a non-blog post thing, Jekyll has already failed”. Recipes are not posts, and we should avoid using them as such if we can.
With that said, here's my approach in three steps:
Create two collections (our custom post types) — let's say these are “Recipes” and “Ingredients”
Find a way to relate them to each other — To make sure “lettuce” will be listed under “salad”
Include the relevant “Ingredients” in your “Recipes” — Adding the liquid code to actually display information from “lettuce” somewhere in the “salad” page.
Step 1
Create _ingredients and _recipes folders for each of your collections (make sure to name them in plural) and add them to your _config.yml file:
collections:
ingredients:
output: true
recipes:
output: true
permalink: /recipes/:path/
The output: true parameter creates an individual HTML page for each item in the collection and permalink (optional) lets you control the URL.
Step 2
Add a piece of metadata like this to the YAML front-matter of your lettuce.md collection item:
---
parent-collection: salad
---
If lettuce.md belongs to more than one recipe, you can add more than one. Make sure to add content below the metadata if you need a description:
---
parent-collection:
- salad
- hamburguer
- taco
---
Lettuce is really good for your health, but it kinda sucks as a food.
Note that salad, hamburguer and taco should named exactly like the recipe URL slug or this won't work (i.e. greatrecipes.com/recipes/salad). If you have a recipe name that's a sentence, you wrap it in quotes.
This variable will be slugified later on, so a name like parent-collection: The Amazing Souflè Français will match the-amazing-soufle-francais. Still, weird stuff happens: when in doubt, just write smallcaps salad.
This is the hacky part.
Step 3
Create a specific layout for your recipes (like _layout/recipe-page.html) and add the code below — this is where we'll include the ingredients into the recipe page.
Remember your recipes (salad.md and friends) should be pointing to this layout.
{% capture current_collection %}{{ page.url | remove: "recipes" | remove: "/" | remove: " " }}{% endcapture %}
Here we capture the name of the recipe from the URL and assign it to the current_collection variable. Make sure this in a single line because making it multi-line will add a bunch of whitespace to the captured string and break your code 10/10 times. Insane.
<div class="recipe-content">
<p>{{ content }}</p> -- This is the content of the current recipe in the collection, your "post text".
</div>
{% for ingredient in site.ingredients %}
{% capture captured_parent_collection %}{{ ingredient.parent-collection | slugify }}{% endcapture %} -- This capture is just to pass the slugify filter and sanitize parent.collection to make sure it matches the URL slug
{% if captured_parent_collection == current_collection %}
<div class="recipe-ingredient">
<h3>{{ ingredient.name }}</h3> -- "<h3>Lettuce</h3>"
<p>{{ ingredient.content }}</p> -- "<p>Lettuce is really good for your health, but it kinda sucks as a food.</p>"
</div>
{% endif %}
{% endfor %}
Match the ingredients for this recipe and includes each one of them into the layout. YASS!
Each of your ingredients should be appearing right under your recipe text.
Note that you're not pulling the ingredients inside the recipe text like you described in your question, but including them in the layout after it. As far as I know you can't really add things in the middle of your post/collection text — unless you hack your way through a custom plugin for Jekyll.
There are certainly less hacky ways to do this, but this is how I made it work. Let me know if you find any trouble trying this for yourself — and anyone else, feel free to correct me.
I have this setup going on in my site — take a look at the layout where I pull in my child collection items and the metadata on the child collection items themselves.
I stumbled upon this question while looking for a way to do this, and here's what I ended up doing. I figured I'd post it in case somebody else is looking for a way to do this as a one-off and doesn't feel like creating a whole collection.
In the post where you want to include your other post, you can use Jekyll's where filter to get a reference to your other post, and then print that post's content:
---
layout: whatever
---
Your original post's content here.
{% assign myOtherPost = site.posts | where:"url", "/other/post/url" | first %}
{{ myOtherPost.content }}
Some more content here.
currently I'm working on a new site with Jekyll and have some kind of problem there. I have a layout page, where I can define the background image with variables from each page.
Layout:
class="background background-{{ page.header_bg }}"
Page:
---
header_bg: storm
---
But now I want to include some file dynamically, depending on the variable value. Well, I can do it with some if or case statements, but actually I want to do something like
{% include page.header_bg %}
But this does not work, because Jekyll is looking for a file, that is called "page.header_bg" and not the value.
Can some one help me please?
According to the docs, you need to put the variable name inside additional {{ }}.
Quote from the link:
ProTip™: Use variables as file name
The name of the file you wish to embed can be literal (as in the
example above), or you can use a variable, using liquid-like variable
syntax as in {% include {{my_variable}} %}.
I know this question is 6 years old but here's a clear answer for posterity. You need to assign page.header_bg to a variable first and then use the variable in the include statement.
{% assign headerBg = page.header_bg %}
{% include headerBg %}
You will get an error Liquid Exception: Invalid syntax for include tag if you attempt to use page.header_bg directly in the include statement.
Hi I am trying to evaluate a variable from a file and a normal one but seems to be harder than it looks so :
This works:
{config_load file="archive_page.conf"
section="profile"} {include file="header.tpl" title=#pageTitle# keywords=#keywords# description=#description#}
I would like to also use my var and concatenate the text together so the below doesn't work also I have tried variations with '', "" but leads either an error message or one of the variables to display as text...
{config_load file="archive_page.conf"
section="profile"} {include file="header.tpl" title=#pageTitle#$MYVARHERE keywords=#keywords# description=#description#}
I tried various things but I can't get it to work, any help is much appreciated.
use the cat variable modifier:
title=#pageTitle#|cat:$MYVARHERE
how can i write code blocks in maruku
for ruby,javascript
currently i am using technique. but my first line moving to left.
hash["test"] = "test"
hash.merge!("test" => "test")
h=HashWithIndifferentAccess.new
h.update(:test => "test")
{:lang=ruby html_use_syntax=true}
I'm not sure I fully understand the question, but Maruku is just a Markdown interpreter.
To produce a code block in Markdown, simply indent every line of the block by at least 4 spaces or 1 tab. For example, given this input:
This is a normal paragraph:
This is a code block.
Markdown will generate:
<p>This is a normal paragraph:</p>
<pre><code>This is a code block.
</code></pre>
I add this answer because I ended up here searching for a solution to code blocks with Maruku using Jekyll. For anyone else in the same boat, use the Liquid tags for code blocks instead of the Markdown syntax:
{% highlight java %}
System.out.println("Hello, Maruku.");
{% endhighlight %}
Also see this question/answer: Highlight with Jekyll and pygments doesn't work