packlat.blogg.se

Variables pug template
Variables pug template














Extending templates must have at least one block, or it would be empty - just define your variables there.įor the same reason, Pug’s buffered comments cannot appear at the top level of an extending template: they produce HTML comments which would have nowhere to go in the resulting HTML. Define the variables in a block in the child template.The child template will inherit these variables.

Variables pug template code#

  • Add the variables to the Pug options object, or define them in unbuffered code in a parent template.
  • If you need to define variables for use in a child template, you can do so a few different ways: This includes unbuffered code, which can also contain markup. If a child template tried to add content outside of a block, Pug would have no way of knowing where to put it in the final page.

    variables pug template

    This is important! Parent templates define a page’s overall structure, and child templates can only append, prepend, or replace specific blocks of markup and logic. Note that only named blocks and mixin definitions can appear at the top (unindented) level of a child template. However, if you chain many, many templates together, you can make things a lot more complicated for yourself. Pug’s template inheritance is a powerful feature that allows you to split complex page template structures into smaller, simpler files.

    variables pug template

    When using block append or block prepend, the word “ block” is optional: //- page.pug You can simply append the block: //- page.pug You want some game related scripts as well as these defaults. Now, consider a page of your JavaScript game. Suppose you have default scripts in a head block that you wish to use on every page. Pug allows you to replace (default), prepend, or append blocks. (Alternatively, the child template could override content altogether.) //- sub-layout.pug

    variables pug template

    As it shows, content now exposes a sidebar and primary block for overriding. It’s also possible to override a block to provide additional blocks, as shown in the following example. pug is automatically appended to the file name.) Then, define one or more blocks to override the parent block content.īelow, notice that the foot block is not redefined, so it will use the parent’s default and output “some footer content”. To extend this layout, create a new file and use the extends directive with a path to the parent template. The example below defines block scripts, block content, and block foot. Providing default content is purely optional, though. Pug blocks can provide default content, if appropriate. In a template, a block is simply a “block” of Pug that a child template may replace. Template inheritance works via the block and extends keywords.














    Variables pug template