How to use custom option form grunt task in loops - handlebars.js

I am using grunt-assemble, in grunt task I add custom option of language by following below documentation
assemble docs
and below is the image of my grunt task
grunt task
{{language}}
{{#withSort pages "data.navSortOrder"}}
{{langugae}}
{{#is data.showInNav true}}
<li{{#is ../../page.dest this.dest}} class="active"{{/is}}>
{{language}}
{{/is}}
{{/withSort}}
In above code language outside the withsort block is outputting the expected result but in withsort block language option isn't outputting anything

When using block helpers like withSort, handlebars changes the depth of the data. To access the previous depth (where language is), you can use the .. syntax. If you know language will always be at the root of the context, then you can use the #root keyword:
{{language}}
{{#withSort pages "data.navSortOrder"}}
{{../language}}
{{#root.language}}
{{#is data.showInNav true}}
<li{{#is ../../page.dest this.dest}} class="active"{{/is}}>
{{language}}
{{/is}}
{{/withSort}}
Also... when updating your example, I noticed that the second "language" was spelled as "langugae".

Related

Using Hugo, how can I access a variable from a partial file that is defined in base file?

I'm new to using Hugo and Go Templates. How can I access a variable from a partial file that is defined in base file using Hugo?
For eg: I have an index.html file which contains code that reads the data stored in the events.json file in the data directory and stores it in a variable. How can I access that variable from another file?
index.html
{{ $events := .Site.Data.events }}
{{ partial "people" . }}
people.html
// access the events variable from the index.html
{{ $events }}
I really hope this makes sense. I can try and clarify more if needed.
0.15 introduced a map that can be used for this.
index.html
{{ $events := .Site.Data.events }}
{{ partial "people" (dict "events" $events) }}
people.html
// access the events variable from the index.html
{{ .events }}
You could use the dict func:
{{ partial "people" (dict "page" . "events" $events) }}
You will then address them like {{ .page.someVar }} and {{ .events.someVar }} in the partial.
An alternative in your case could maybe, in the partial (as the previous poster said), address the .Site.Data.events directly from the partial.
According to Hugo documentation:
... partial calls receive two parameters.
The first is the name of the partial and determines the file location to be read.
The second is the variables to be passed down to the partial.
This means that the partial will only be able to access those variables. It is isolated and has no access to the outer scope.
This means, the events variable is outside of the scope of people.html. Your people.html cannot "see" it. One solution would be pass it down, like:
{{ partial "people" . $events }}
If it does not work, try different notation ($ vs. .).
If that does not work, you can always call your data file again, without variable, just like in the examples, that is, use {{ .Site.Data.events }} in your people.html partial.
Let me know in the comments how it goes, I'll try to improve my answer if necessary. I know it's a pain to get out of Hugo boundaries into Go territory :)

How do I include a variable in an "ifequal" handlebar?

I'm using Foundation for Sites which uses Panini and Handlebars for JS templating. I need an {{#ifequal}} statement that includes a js variable so that I can compare a URL parameter to a JSON
What I have right now:
{{#ifequal "Canada" this.destination}}
//do the thing
{{/ifequal}}
What I need is something like this:
{{#ifequal <script>document.write(destId)</script> this.destination}}
//do the thing
{{/ifequal}}
The js var "destId" is assigned earlier in the page when it's pulled out of the URL parameters, but of course I can't include the script inside the handlebars. If there's a way to pass a URL parameter directly into a handlebar that would also work.
as noted before on this question (link is here):
Handlebars partials take a second parameter which becomes the context for the partial:
{{> person this}}
In versions v2.0.0 alpha and later, you can also
pass a hash of named parameters:
{{> person headline='Headline'}}
You can see the tests for these
scenarios:
https://github.com/wycats/handlebars.js/blob/ce74c36118ffed1779889d97e6a2a1028ae61510/spec/qunit_spec.js#L456-L462
https://github.com/wycats/handlebars.js/blob/e290ec24f131f89ddf2c6aeb707a4884d41c3c6d/spec/partials.js#L26-L32

In Jekyll, can we group multiple collections inside the same folder?

In config.yml, I define my collections like this:
collections:
music:
output: false
dancing:
output: false
The problem is I will have lots of collections and they will clutter my root Jekyll folder.
Is there a way to group all the collections into a folder, named for example, _collections?
So, I would have:
_collections
_dancing
_music
....
This is now possible (I'm running Jekyll 3.7.2, I'm not sure in which version this was implemented).
Here's how to do it: In your _config.yml you can define your collections as well as the folder for your collections. Let's take a look at an example on a client site I'm working on:
collections:
events:
output: true
work:
output: true
jobs:
output: true
cases:
output: true
permalink: /work/:name
collections_dir: pages
The collections_dir: [your_folder_here] will tell Jekyll to look into that folder for collections. My folder structure in development is as follows:
pages/
...
_events/
_work/
_jobs/
_cases/
And in the compiled site it's as follows:
...
events/
jobs/
work/ (contains both "work" and "cases" collections)
One thing also that wasn't asked, but I found to be useful, was that you're able to output different collections into a same folder. In my case I had a client website on which there were two types of work samples: client cases and general examples. We wanted to separate them for better maintenance but also show them in the same folder. To achieve this you can simply define a permalink for the collection. In our case we put permalink for the cases to appear in the work folder (permalink: /work/:name).
Hope this helps!
This is also present in the Jekyll documentation
Answer is no. Your collections folder must be at the root of your root folder.
Even if you name you create a collection in _collections/_music folder, and set it up like this :
collections:
collections/_music folder:
output: true
Jekyll ends up looking for your collection in _collections_music folder (without any slash) because of path sanitize process.
See jekyll code in collection.rb, site.rb and jekyll.rb
This is now possible since this issue was merged.
User configures as:
collections_dir: my_collections
Then we look in my_collections/_pizza for the pizza collection, and my_collections/_lasagna for the lasagna collection.
Nothing prevents you to use subfolders in your collection.
(Note: this is not an answer to your question but a possible workaround)
So as a workaround you could have just one collection: say _arts for example and organize your folders like:
_arts
dancing
music
concerts
.....
to list them separately you can use:
a FrontMatter variable in your files category: dancing when you have a output and check this for a dancing only list for example.
{% for project in site.arts %}
{% if project.category == 'dancing' %}
....
{% endif %}
{% endfor %}
or check the path when you have no output for the collection
{% for project in site.arts %}
{% if project.url contains 'dancing' %}
....
{% endif %}
{% endfor %}
This could slowdown your build if you have hundreds and hundreds of items inside.

Registering multiple partials directory in handlebars assemble 0.7.3

Is it possible to register multiple directories for partials namespaced by the directory they are in?
For example in gulp-hb is is possible to register multiple locations for partials like this
partials: [
'./source/templates/partials/**/*.hbs',
'./source/templates/layouts/**/*.hbs'
],
Then these partials can be referenced like this
{{#extend "layouts/master"}}
{{#content "body"}}
{{> "partials/header"}}
{% body %}
{{/content}}
{{/extend}}
I have tried using handlebar-layouts with assemble but it seems to look only in the partials folder and not in the templates folder too.
This is my folder structure
source/
templates/
layouts/
article.hbs
master.hbs
partials/
header.hbs
Any help is much appreciated.
You can setup a renameKey function on the partials collection and do whatever you'd like with the filepath that comes through:
app.partials.option('renameKey', function(fp) {
// do some logic here to figure out the folder the partial is in.
// this is a simple example and will only work for 1 level deep
var dir = path.dirname(fp);
return dir + path.basename(fp, path.extname(fp));
});

NoReverseMatch when using Apphook without namespace

I use Django CMS 3 and Django 1.6 and the default django polls app , and I am doing this short tutorial.
My problem is that the PollsApp works fine when it's using namespace like this:
djangocms_polls/cms_app.py:
...
class PollsApp(CMSApp):
name = _("Poll App")
urls = ["polls.urls"]
app_name = "polls"
...
polls/templates/polls/index.html:
...
{% for poll in latest_poll_list %}
<li>{{ poll.question }}</li>
{% endfor %}
...
But when I delete the "polls:" part from the index.html, it won't work (and it doesn't matter if there is or isn't app_name field in PollsApp) and I get this Error:
NoReverseMatch at /polls/
Exception Value:
Reverse for 'detail' with arguments '(1L,)' and keyword arguments '{}' not found. 0 pattern(s) tried: []
Why am I making my life hard when everything works - you ask? It's because I want to use apps that doesn't use namespaces like django-shop and when I created apphook for django-shop - the same problem occured.
When you're using apps via an Apphook in CMS you have to supply a namespace to when creating URLs in the templates.
In general also, when creating apps I believe it to be from a 'best practice' approach to always namespace your app and template URLs.
If you're using an app which doesn't make use of the app_name Meta attribute, you can define a namespace when you include an app's URLs in your root urls.py. Take a look over the example here; https://docs.djangoproject.com/en/1.7/topics/http/urls/#reversing-namespaced-urls

Resources