Eleventy, frontmatter data not being evaluated - yaml-front-matter

I have a template that looks like this:
---
date: "2016-01-01T06:00-06:00"
value: "/{{ page.date | date: '%Y/%m/%d' }}/index.html"
---
Value prints: {{ value }} <br/>
But we expect: {{ page.date | date: '%Y/%m/%d' }}/index.html <br/>
When I render the site then the site looks like this:
Value prints: /{{ page.date | date: '%Y/%m/%d' }}/index.html
But we expect: 2016/01/01/index.html
I really want the value parameter to have the expected value.
As far as I can tell, this sort of thing should work. I want to use this technique to calculate permalinks. My thinking is based on https://www.11ty.dev/docs/permalinks/
I'm running eleventy 0.12.1
Things I've tried:
yaml, json and js frontmatter
markdown template
njk template
literally copy pasting sample code from the docs
At this point I think Eleventy might have a bug

At the moment of writing, eleventy doesn't support template syntax in any frontmatter fields except the permalink field:
permalink: Change the output target of the current template. Normally, you cannot use template syntax to reference other variables in your data, but permalink is an exception.
Source
Instead, you can use computed data, which allows you to set frontmatter data based on other frontmatter fields. Something like this should work:
date: "2016-01-01T06:00-06:00"
eleventyComputed:
value: "/{{ page.date | date: '%Y/%m/%d' }}/index.html"

Related

How to use Thymeleaf layout dialect with Textual Syntax?

I'm trying to use the layout:decorate directive with a text file (plain text email header/footer)
Template begin called:
[# layout:decorate="~{email/plainPart}" th:with="subject=${subject}" ]
[# layout:fragment="content"]
Thanks for ordering [( ${orderDetail} )].
[/]
[/]
Layout template:
Subject: [( ${subject} )]
[# layout:fragment="content"][/]
The temple renders fine, and without any errors, but I'm not seeing any of the layout template (Subject, for example).
I've been unable to get layout:decorate to work. But based on a Stack Overflow answer about th:replace, I've been able to come up with a sort of solution:
Template:
[#th:block th:replace="email/plainPart.txt"][/th:block]
Thanks for ordering [( ${orderDetail} )].
plainPart.txt:
[( ${subject} )]

How can I get Ractive.js to cooperate with Slim templates?

The Ractive tutorial uses this:
<th class='sortable {{ sortColumn === "name" ? "sorted" : "" }}'
on-tap='sort:name'>
Superhero name
</th>
The project I am working on uses Slim. Using html2slim, I am provided with this syntax:
th.sortable.sortColumn.: class=("{{ === \"name\" ? \"sorted\" \"\" }}") on-tap="sort:name"
Superhero name
I don't know if that is valid Slim syntax; I cannot find anything in the Slim documentation to guide me. So I'm lost as to how this should be formatted in Slim, to render properly for Ractive.
The above syntax results in:
syntax error, unexpected tIDENTIFIER, expecting keyword_end
I have searched for gems, SO answers, and broad Googling but cannot find any clues. Has anyone here successfully done something like this?
In slim, use a double equal == to disable escaping in the attribute, see https://github.com/slim-template/slim#quoted-attributes
I believe you can also mix ' and "" to avoid back-slashing, and I think you need to include the data ref in the mustache. The : inline a child element, so you don't want that either. Lastly, I'm not sure you can mix .classname notation with an explicit attribute. So I think it ends up being:
th class=="sortable {{ sortColumn === 'name' ? 'sorted' : '' }}" on-tap="sort:name"
Superhero name

Regular expression to determine each and every attribute of an anchor tag inside HTML content

I basically wanted the values of each and every attribute. The attributes may be optional and the href may contain HTTP or HTTPS.
A sample anchor tag inside content is:
<a class=\"direct_link\" rel=\"nofollow\" target=\"_blank\" href=\"http://google.com\">link text</a>
Sample HTML content is:
<p><br></p><h1>A beautiful <a class=\"f-link\" rel=\"nofollow\" target=\"_blank\" href=\"fake.com/abc.html\">jQuery</a>; a</h1><h3 class=\"text-light\">Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's.</h3><p><br></p><p><br></p>
Don't use a regular expression to try to parse HTML. HTML can be expressed too many ways and still be valid, yet it will break your pattern and code.
The correct way to get the values for the parameters is to use a parser. Nokogiri is the defacto XML/HTML parser for Ruby:
require 'nokogiri'
doc = Nokogiri::HTML::DocumentFragment.parse(' <a class=\"direct_link\" rel=\"nofollow\" target=\"_blank\" href=\"http://google.com\">link text</a>')
That parses the document into a DOM and returns it.
link = doc.at('a')
at finds the first instance using the CSS 'a' selector. (If you want to iterate over them all you can use search, which returns a NodeSet, which is akin to an Array.)
At this point link is a Node, which we can consider to be like a pointer to the <a> tag.
link.to_h # => {"class"=>"\\\"direct_link\\\"", "rel"=>"\\\"nofollow\\\"", "target"=>"\\\"_blank\\\"", "href"=>"\\\"http://google.com\\\""}
That is the link's parameters and their values turned into a hash. Or, you can directly access the parameters, using keys, or their values:
link.values # => ["\\\"direct_link\\\"", "\\\"nofollow\\\"", "\\\"_blank\\\"", "\\\"http://google.com\\\""]
link.keys # => ["class", "rel", "target", "href"]
Or treat it like a hash and iterate over the key/value pairs:
link.each do |k, v|
puts 'parameter: "%s" value: "%s"' % [k, v]
end
# >> parameter: "class" value: "\"direct_link\""
# >> parameter: "rel" value: "\"nofollow\""
# >> parameter: "target" value: "\"_blank\""
# >> parameter: "href" value: "\"http://google.com\""
The advantage to using the parser, is that the HTML format can change and the parser is still able to figure it out, and your code won't care. The following format works just as good as the tag used above:
doc = Nokogiri::HTML::DocumentFragment.parse(' <a
class=\"direct_link\"
rel=\"nofollow\" target=\"_blank\"
href=\"http://google.com\">
link text
</a>')
Try doing that with a pattern.
Well if you want does the stuff in the quotes it would be this:
"([\w:\/.]+)\\"
Test it here
Otherwise if you want the name before the quotes it would be this:
(\w+=\\"[\w:\/.]+\\")
Test it here
This one matches tags without backslashes:
(\w+="[\w:\/.-]+")
Test it here

Phalcon create Link with get parameters

Is there a helper function in phalcon (volt) to create Links to routes with GET-Parameters ? I have pagination-links on which I want to add ?cat=category and ?year=year depending on whether they are set or not.
First
Previous
Next
Last
so that
http://site.tld/tags/xyz?page=2
would become:
http://site.tld/tags/xyz?cat=a&year=b&page=2
or this, if cat is not set or null:
http://site.tld/tags/xyz?year=b&page=2
edit
this way it seems to work:
First
Previous
Next
Last
the rest happens in the controller
IMO it's easier to do that in the controller than using volt.
First, generate the base URL for your pagination links with the URL Service:
$pagingUrl = $this->url->get('tags/' . $tagname->tag);
Now you can get 'cat' and 'year' with something like $this->request->getPost('cat'); to check if it's set and append it to $pagingUrl as GET parameters. Leave a '&page=' at end of the $pagingUrl to easily append the page number later.
Set $page and $pagingUrl as variables for your view so you can easily access it from volt:
$this->view->setVar('page', $page);
$this->view->setVar('pagingUrl', $pagingUrl);
Finally in the view you could use something like that:
{{ link_to(pagingUrl ~ '1', 'First') }}
{{ link_to(pagingUrl ~ page.before, 'Previous') }}
{{ link_to(pagingUrl ~ page.next, 'Next') }}
{{ link_to(pagingUrl ~ page.last, 'Last') }}
EDIT
The solutions above seems hackish because Phalcon designers aimed to work more with clean URLs than explicit GET parameters. If you were passing your parameters this way, your TagController could have an action that supports pagination like this:
class TagController
{
...
public function ListAction($page = 1, $category = 'default-cat', $year = 1997)
{
...
Working that way you can easily create links like these:
tags/list
tags/list/2/stuff
tags/list/9/stuff/2014

dust js partials not working inside loop

I'm trying to do something similar to the below code.(trying to print an partial inside a loop)
{#projects
{<greeting}
Hello {.name}
{/greeting}
{/projects}
But im getting an output like below:
Hello
Hello
....
As you can see that partial is not printed inside the loop.
My actual code http://jsfiddle.net/WKxzb/1/
Most of the documentation examples suggest something like this fiddle: http://jsfiddle.net/cnftm/
In short, in greeting.tl:
Hello, {name}!
then in projects.tl:
{#projects}
{>greeting /}
{/projects}
If where you store name didn't happen to be named name:
{#projects}
{>greeting name=someOtherVar /}
{/projects}
Anything you want to be parametrized has to be, ahem, passed as a parameter, and no body is supported.
The exception to these is blocks with inline partials, which are like this fiddle: http://jsfiddle.net/PWYBr/1/
In short, in greeting.tl:
Hello, {+name}Anonymous Coward{/name}!{~n}
then in projects.tl:
{#projects}
{>greeting/}
{<name}
{name}
{/name}
{/projects}
This works great for loops, but you can't reuse the greeting partial ANYWHERE else on the page... basically, whoever writes {<name} last wins, such that this will do weird things:
{>greeting/}
{<name}everybody{/name}
{#projects}
{>greeting/}
{<name}
{name}
{/name}
{/projects}

Resources