Is it possible to use template parameters in the content of a post with Hugo? E.g. if I have the following parameters:
[params.company]
name = "My Company"
Can I then do something like this in the content of a post?
This site, {{ .Site.BaseURL }} is operated by {{ params.company.name }}
I've tried, but it's literally printing the above instead of interpolating the variables.
1. Front matter way
As far as I'm aware, it's not possible* to put variables within the markdown file's content because MD parser would strip them, but it's possible to do it using custom variables on the front matter of each .md content file. The Hugo engine can target any fields you set in front matter. Front matter fields can be unique as well.
In your case, the template which is called to show the rendered .MD file has access to front matter parameters and you can change template's behaviour (like add classes of extra <div>'s) or even pull the content right from the parameter.
For example, on my .md files I have:
---
title: "Post about the front matter"
<more key pairs>
nointro: false
---
The key nointro: true would make my first paragraph to be normal size. Otherwise, if key is absent or false, first paragraph would be shown at larger font size. Technically, it's adding a custom class on a <div>.
In the template, I tap into the custom parameter nointro this way:
parent template which shows your markdown file, which has front matter parameters:
<div class="article-body {{ if .Params.nointro }} no_intro {{ end }}">
{{ .Content }}
</div><!-- .article-body -->
Notice I can't put variables within {{ .Content }}, but I can outside of it.
For posterity, that's piece of the content from a file hugo/themes/highlighter/layouts/partials/blog-single-content.html, it's a partial for single post content. You can arrange your partials any way you like.
Obviously, that's Boolean parameter flag, but equally it could be content which you could use directly:
MD file's top:
---
title: "One of our clients"
<more key pairs>
companyname: "Code and Send Ltd"
---
Text content is put here.
Then, reference it like this (extra insurance against blank value using IF):
Anywhere in Hugo template files:
{{ if .Params.companyname }}{{ .Params.companyname }}{{ end }}
2. Using config.(toml/yaml/json)
Now, looking at your example, "This site is operated by " would almost warrant a custom field in more global location, for example, hugo/config.toml. If I wanted to add a companyname into my config, I'd do it this way:
hugo/config.toml:
BaseURL = "_%%WWWPATH%%_"
languageCode = "en-uk"
title = "Code and Send"
pygmentsUseClasses = true
author = "Roy Reveltas"
theme = "Highlighter"
[params]
companyname = ""
Then I'd use it anywhere via {{ .Site.Params.headercommentblock }}.
I guess if you want your client pages to be static pages then single location might not be the best and you might want to tap into front-matter. Otherwise, if it's a site's footer, this way is better. Alternatively, you could even put this data even on data files.
3. Using custom placeholders and replacing via Gulp/npm scripts
I said not possible*, but it's possible, although unconventional and more risky.
I had such setup when I needed two builds for my website: 1) Prod and 2) Dev. Prod URL's were coming from two places: CDN and my server. Dev had to come from a single place in a static folder because I wanted to see images and was often working offline on a train.
To solve that, I used two custom variables in all my templates (including markdown content): _%%WWWPATH%%_ and _%%CDNPATH%%_. I came up with this unique pattern myself by the way, feel free to adapt it. Then, I put it also on hugo/config.toml as:
hugo/config.toml:
BaseURL = "_%%WWWPATH%%_"
After Hugo happily generated the website with those placeholders, I finished off the HTML's using a Grunt task:
grunt file:
replace: {
dev: {
options: {
patterns: [{
match: /_%%CDNPATH%%_+/g,
replacement: function () {
return 'http://127.0.0.1:1313/'
}
}, {
match: /_%%WWWPATH%%_+/g,
replacement: function () {
return 'http://127.0.0.1:1313/'
}
}...
For posterity, I recommend Gulp and/or npm scripts, I'd avoid Grunt. This is my older code example above from the days when Grunt was the best.
If you go this route, it's riskier than Hugo params because Hugo won't error-out when your placeholder values are missing or anything else wrong happens and placeholders might spill into the production code.
Going this route you should add multiple layers of catch-nets, ranging from simple following Gulp/Grunt/npm scripts step which searches for your placeholder pattern to pre-commit hooks ran via Husky on npm scripts that prevent from committing any code that has certain patterns (for example, %%_). For example, at a very basic level, you would instruct Husky to search for anything before allowing committing this way:
package.json of your repo:
"scripts": {
"no-spilled-placeholders": "echo \"\n\\033[0;93m* Checking for spilled placeholders:\\033[0m\"; grep -q -r \"%%_\" dist/ && echo \"Placeholders found! Stopping.\n\" && exit 1 || echo \"All OK.\n\"",
"precommit": "npm run no-spilled-placeholders"
},
Basically, grep for pattern %%_ and exit with error code if found. Don't forget to escape the code because it's JSON. I use similar (more advanced) setup in production and nothing slips through. In proper setup you should creatively look for anything mis-typed, including: %_, _%, %__, __% and so on.
Normal Go template is not supported in the markdown file, but shortcodes are:
{{< param "company.name" >}}
To access arbitrary other Go template values, create a custom shortcode for it and call that from your markdown file.
For your example, you need the site's baseUrl, so save this as layouts/shortcodes/base_url.html:
{{ .Site.BaseURL }}
And write this in your markdown file:
+++
[company]
name = "My Company"
+++
This site, {{< base_url >}} is operated by {{< param "company.name" >}}
There is also the shortcode param : {{< param "companyName" >}} : https://gohugo.io/content-management/shortcodes/#param
Related
I'm tinkering with blogdown and would like to create figures and table with non-English caption headers. The following chunk
```{r label1, echo=FALSE, fig.cap="Fancy caption", fig.fullwidth=TRUE}
plot(1,1)
```
produces the plot and a caption that reads
Figure 1: Fancy caption
I'd like to be able to change the label such that, say, "Figure" becomes "Plot". I thought I could fix it in the same way as for bookdown: In the _bookdown.yml file I could have
language:
ui:
chapter_name: "Chap "
appendix_name: "App "
label:
fig: 'Plot '
tab: 'Fancy table '
but I'm not sure how to do something similar with a Hugo-based setup from blogdown. How can I add the above information to, say, the config.toml file or set it somewhere else?
First, store the _bookdown.yml file you described in the same folder as the blog post source .Rmd file, e.g. content/post/_bookdown.yml if your file is at content/post/my_post.Rmd.
Then, add _bookdown.yml to the list of ignoreFiles in your config.toml so that Hugo doesn't move _bookdown.yml to the public directory.
This works because blogdown::html_page() is based on bookdown::html_document2(), which will pick up the _bookdown.yml in the same directory of the source Rmd. I don't think it's possible to set this globally from your blogdown root dir, but if you store all your posts in content/post it's basically the same thing.
Is there anyway of uploading Custom Slot Types values via an API, so that you do not need to type them in manually using the new Alexa Skill Builder interface (if you have many of them):
I haven't found anything.
My recommendation is to get the model via SMAPI first, edit the json file with your new values and update it via SMAPI again.
ask api get-model -s "enter your skill id here" --stage development -l en-US > model.json
in the model.json file you can see the slots definition. Change it (with a script or manually) and update the model again
Reference to both commands:
https://developer.amazon.com/docs/smapi/ask-cli-command-reference.html#update-model-subcommand
https://developer.amazon.com/docs/smapi/ask-cli-command-reference.html#update-model-subcommand
It seems not (after searching). There is a feature request logged here with Amazon:
https://forums.developer.amazon.com/questions/9640/api-to-upload-intent-schema-and-sample-utterances.html#answer-77902
Yes you can do this via the SMAPI API/CLI. Take a look at https://developer.amazon.com/docs/smapi/ask-cli-intro.html for a full detail - it allows for full model editing via JSON.
In the left bar bellow the "Intents" and "Slot" is the option "JSON Editor". There you can write a JSON for the new intents you want to add.
Example without slot type
{
"name":"YesIntent",
"samples":[
"Yes",
"Yeah",
"I do",
"I am"
]
}
Example with slot type
{
"name":"NumberIntent",
"slots":[
{
"name":"number",
"type":"AMAZON.NUMBER"
}
],
"samples":[
"{number} is my number",
"{number}",
"my number is {number}"
]
}
As other answers suggest, you can use the SMAPI.
Alternatively, you can select the "code editor" tab on the left and drag/drop or copy/paste your schema json code.
https://github.com/williamwdu/Alexa-Custom-Slot-Generator
I wrote this to convert csv/excel to JSON format so you can paste it into code editor.
Let me know if you have any question.
There is no README for the code coz I have no time these days
I'm creating a document that will help me reduce time down when creating templates. I'm wanting to loop around a block of code in a seperate file with an array that I've specified in the header of the main file but can't seem to get it to work
./src/pug/layout.pug
- const flyouts = []
- flyouts.push({variationid: "123", plugin: "none", zone: "ZoneHere", variation: "LargeImage", link: "www.bbc.co.uk", imgloc: "blah.jpg"});
- flyouts.push({variationid: "456", plugin: "none", zone: "ZoneHere", variation: "LargeImage", link: "www.bbc.co.uk", imgloc: "blah.jpg"});
Calling the Loop here, in the same file
html
head
title Hello asd
body
section
// Flyouts
each myFlyout in flyouts
include components/flyouts
Which calls this block of code in ./src/pug/components/flyouts.pug
.section(variationid=variationid plugin=plugin zone=zone variation=variation)
a(href='asd')
img(src='http' alt="Event" width="206" height="148" border="0")
Anybody able to shine a light on this one?
Jade/Pug keeps the variable scope of the parent template, while including a template. Variable you are looking for here is myFlyout, which is an object with other values - variationid, plugin, etc.
You need to use myFlyout to get values within flyouts.pug. This should work -
.section(variationid=myFlyout.variationid plugin=myFlyout.plugin zone=myFlyout.zone variation=myFlyout.variation)
a(href='asd')
img(src='http' alt="Event" width="206" height="148" border="0")
When creating a language package for atom, it's possible to define a /snippets folder. Any files in here are automatically loaded when the package is active and the context (eg: ".source.js") is opened in the editor.
Now I'd like to contribute to the language-arma-atom package, where there's currently 3 snippet files: I want to add a checkbox option in the package settings to NOT load one of these files.
ie: I know how to add the option, but not how to exclude one of these snippet files.
The way I solved this was create a snippetsAvailable folder, put the files in there (and remove the snippets folder)*.
In your main package file, add to your config schema:
config:
optionalSnippets:
title: "My optional snippets"
description: "Adds optional snippets to autosuggestions"
type: "boolean"
default: true
And in your package's activation do something like this:
activate: ->
#subscriptions = new CompositeDisposable
# etc..
atom.config.observe 'my-package.optionalSnippets', (checked) ->
# For copyNewer, see note below *
copyNewer "my-snippets", "#{__dirname}/snippets", {
cwd: "#{__dirname}/snippetsAvailable"
}
* Note: I used the copyNewer package, because it allows me to remove the /snippets folder, ie: it will automatically create it again. More importantly, it won't overwrite the snippets file on each package activation; Except if you updated your package with new snippets.
Obviously you'll have to write copyNewer = require 'copy-newer' at the top of your main file.
Also, if you choose this method, don't forget to put /settings in .gitignore
Tried to find a solution, but I got always stuck a the docs or at answers include other bundles. In the documentation of the dynamic router you can find the hint:
"Of course you can also have several parameters, as with normal Symfony routes. The semantics and rules for patterns, defaults and requirements are exactly the same as in core routes."
Thats it.
...
/foo/{id}/bar
I tried (seems not) everything to get it done.
Same for all tries:
I tried it to apply a variable pattern and a child route.
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route as PhpcrRoute;
$dm = $this->get('cmf_routing.route_provider');
$route = new PhpcrRoute();
$route->setPosition( $dm->find( null, '/cms/routes' ), 'foo' );
$route->setVariablePattern('/{id}');
$dm->persist( $route );
$child = new PhpcrRoute();
$child->setPosition( $route, 'bar' );
$dm->persist( $child );
$dm->flush();
With or without default value and requirement only '/foo/bar' and '/foo/*' return matches, but '/foo/1/bar' prompts me with a 'No route found for "GET /foo/1/bar"'.
...
Just now I nearly got it done.
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\Route as PhpcrRoute;
$dm = $this->get('cmf_routing.route_provider');
$route = new PhpcrRoute();
$route->setPosition( $dm->find( null, '/cms/routes' ), 'example_route' );
$dm->persist( $route );
$route->setPrefix( '/cms/routes/example_route' );
$route->setPath( '/foo/{id}/bar' );
$dm->flush();
If prefix is '/cms/routes' and name is 'foo' everything works fine. But now that I got this far, assigning a speaking name would round it up.
Thanks in advice!
You got quite close to the solution, actually!
When using PHPCR-ODM, the route document id is its path in the repository. PHPCR stores all content in a tree, so every document needs to be in a specific place in the tree. We then use the prefix to get a URL to match. If the prefix is configured as /cms/routes and the request is for /foo, the router looks in /cms/routes/foo. To allow parameters, you can use setVariablePattern as you correctly assumed. For the use case of /foo/{id}/bar, you need to do setVariablePattern('/{id}/bar'). You could also have setVariablePattern('/{context}/{id}') (this is what the doc paragraph you quoted meant - i will look into adding an example there as its indeed not helpful to say "you can do this" but not explain how to).
Calling setPath is not recommended as its just less explicit - but as you noticed, it would get the job done. See the phpdoc and implementation of Model\Route::setPattern:
/**
* It is recommended to use setVariablePattern to just set the part after
* the static part. If you use this method, it will ensure that the
* static part is not changed and only change the variable part.
*
* When using PHPCR-ODM, make sure to persist the route before calling this
* to have the id field initialized.
*/
public function setPath($pattern)
{
$len = strlen($this->getStaticPrefix());
if (strncmp($this->getStaticPrefix(), $pattern, $len)) {
throw new \InvalidArgumentException('You can not set a pattern for the route that does not start with its current static prefix. First update the static prefix or directly use setVariablePattern.');
}
return $this->setVariablePattern(substr($pattern, $len));
}
About explicit names: The repository path is also the name of the route, in the example /cms/routes/foo. But it is not a good idea to use a route name of a dynamic route in your code, as those routes are supposed to be editable (and deletable) by an admin. If you have a route that exists for sure and is at a specific path, use the configured symfony routes (the routing.yml file). If its dynamic routes, have a look at the CMF Resource Bundle. It allows to define a role for a document and a way to look up documents by role. If you have a route with a specific role that you want to link to from your controller / template, this is the way to go. If you have a content document that is linked with a route document and have that content document available, your third and best option is to generate the URL from the content document. The CMF dynamic router can do that, just use the content object where you normally specify the route name.