Is there a way to define custom "Actions" (like range, if, block, etc) with either text or html go templates. I would like to achieve something like the following:
{{ component "blog_post" . }}
{{ template "title" . }}
{{ component "content" . }}
My Content
{{ end }}
{{ end }}
Where "component" is my custom action.
I have achieved the above functionally using custom functions but it is pretty clunky and hard to read. I am particularly interested in the ability to use a custom action that takes both a normal argument (such as .) as well as arbitrary "children" (much like a react component).
This is not possible in the current implementation of Go templates.
Actions are handled by the internal text/template/parse package, and are all defined in https://golang.org/src/text/template/parse/node.go. You will find that there is no mechanism for creating new actions, as the Tree.action method in parse.go has a hardcoded switch case for them.
This is sad because the text/template package has a lot of missed opportunities for reusable features. For example, there's an entire built-in LISP parser that's unusable unless you use it through the template parser.
Related
Wondering if it is possible to manipulate the output of a #yield in Laravel? In short I have a title in my child template that I would like to set some id's with to make them unique. I know I could just create another #section('id', 'asdf') but rather not have to worry users with that especially since the title has to be unique anyways...
Could not find anything that says this is possible?
Example (which fails) but what I am essentially trying to do:
id="{{Str::kebab(#yield('title'))}}-preview-tab"
#yield gets replaced with a PHP echo statement so that is not what you want. If you want the content of a section you can grab it from the View Factory:
$__env->getSection($name, $default)
Or even calling yieldContent:
$__env->yieldContent($section, $default)
So you could try:
{{ Str::kebab($__env->getSection('title', 'some default if you want')) }}
If you have any issues with that, try the yieldContent method.
I've made an implementation of vue.js in an .net-core mvc project which will return data from a controller. So let's say that the result is:
[{"id":1,"uniqueStr":"string1","ttlValue":"something","hlpValue":"something2"},{"id":2,"uniqueStr":"string2","ttlValue":"something","hlpValue":"something2"}]
I can get the row value with a snippet like:
{{ data[0] }}
So I have two things remaining. The situation is, I need the rows on one page without a loop. So my question would be:
How can I get the "ttlValue" of the row where the "uniqueStr" would be "string2".
I thinking something in the line of but that obviously doesn't function:
{{ data[0][1] }}
I would do it with a computed value, like this. If you need to pass a parameter, you can use a method instead, like this.
I'm not sure what you meant with without a loop though. My solution loops through the object with .filter and with your current data structure, it's not really doable more directly.
I'm building a web page using Poole/Lanyon. A file uses a multi-level or nested site variable like {{ site.github.repo }}.
As a novice in Ruby and YAML, I'm not sure how I should declare this value in _config.yml. I tried like this in _config.yml:
github:
- repo: 'https://github.com/foo/bar.github.io'
It was not working: an empty string is returned when I use {{ site.github.repo }}. I'm however able to get {{ site.github }} like this:
{"repo"=>"https://github.com/foo/bar.github.io"}
In order to use site.github.repo, how should I define this variable in the configuration file?
In your _config.yml, you've defined site.github as a list, and you're trying to access it as an associative array, hence the problem.
If you want to access it as an associative array, you'll need to redefine your variable as such:
github:
repo: 'https://github.com/foo/bar.github.io'
As of writing this, I don't think that the Wikipedia sections I have linked to are super clear, but you can refer to their sample document, which I think showcases YAML's possibilities pretty well.
I'm developing a plugin to facilitate multilingual Jekyll sites, and as part of this I have to categorise posts according to their language.
I'm trying to tag the post according to its language, so I have overwritten the aggregate_post_info method, but when I print the site.tags variable, it is empty.
module Jekyll
class Site
alias_method :_aggregate_post_info, :aggregate_post_info
def aggregate_post_info(post)
_aggregate_post_info(post)
#tags[post.data['lang']] << post
end
end
end
I have achieved something similar by defining my own language specific variables, like in this simplified example:
for post in site.posts.docs do
lang = post.data['lang']
for tag in post.data['tags'] do
slug = jekyll_tagging_slug(tag)
site.config['t'][lang]['tagnames'] = slug
end
end
I also automatically generate tag pages (and category pages using a different approach without plug-ins), avoid name collisions for multiple languages, and precompute aggregate counts for better performance. The whole thing is described in two blog posts http://www.guido-flohr.net/multilang-with-jekyll/ and http://www.guido-flohr.net/jekyll-multilang-tags/.
You can simply add the post language as a tag, i.e. tags: [english, ruby, etc], avoiding altogether the monkey patching. So, when you want to show only lang-tagged posts, you simply filter them:
<ul class="posts">
{% for post in site.tags.english %}
<li>
{{ post.title }}
</li>
{% endfor %}
</ul>
This way, most of the work is done by Jekyll, saving you some time and effort. :)
Every time a tpl file is included the system first looks for a site-specific version of the file and falls back to a standard one if the site specific one does not exist. So perahps I put include "customer/main/test1.tpl". If our site is google, the system would first look for "customer/main/google_test1.tpl" and fall back to "customer/main/test1.tpl" if that file doesn't exist.
Note: Smarty 2.6.x
Did you know about the built-in template directory cascade? addTemplateDir and setTemplateDir allow you to specify multiple directories:
$smarty->setTemplateDir(array(
'google' => 'my-templates/google/',
'default' => 'my-templates/default/',
));
$smarty->display('foobar.tpl');
Smarty will first try to find my-templates/google/foobar.tpl, if not found try my-templates/default/foobar.tpl. Using this, you can build a complete templating cascade.
That won't be too helpful if you have lots of elements on the same cascade-level. Say you had specific templates for google, yahoo and bing besides your default.tpl. A solution could involve the default template handler function. Whenever Smarty comes across a template it can't find, this callback is executed as a measure of last resort. It allows you to specify a template file (or template resource) to use as a fallback.
So you could {include}, {extend}, ->fetch(), ->display() site_google.tpl. If the file exists, everything is fine. If it doesn't, your callback could replace _google with _default to fallback to the default template.
If neither template_dir cascade nor default template handler function seems applicable, you'll want to dig into custom template resources.