How to insert html block in Handlebars partials - handlebars.js

I use Handlebars for a simple Node.js project and I would like to insert more than just few variables in a partial.
I have a layout that contains the very basic frame of my html page (html and head tags). The pages I render are usually all the same and contain the following code:
{{> default/header
help_title="This is the title of my help modal."
help_body="<p>This is the body.</p>"
}}
<main class="vertical-center">
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-12 col-lg-8 justify-content-center align-items-center">
<!-- THE CONTENT THERE -->
</div>
</div>
</div>
</main>
{{> default/footer}}
Depending on the page itself, I may want not to display the header and/or the footer (and I remove the line consequently).
My need: I'd like to wrap the main part in a partial (because everything but the <!-- THE CONTENT THERE --> is invariant) or whatever could do the job and providing to it not only few words (like in the header) but potentially a lot of html code. And I cannot find a way to do it in the documentation. Something like this, that would basically work a bit like layouts:
{{> default/main
<!-- THE RAW HTML CONTENT THERE -->
}}

Handlebars supports exactly what you are describing in the form of partial blocks. The default behavior of partial blocks is to render default content if the partial cannot be found. However, there is a special built-in helper, #partial-block which will inject content into your partial.
From the documentation:
This block syntax may also be used to pass templates to the partial, which can be executed by the specially named partial, #partial-block.
Inside your partial, you will add {{> #partial-block }} where you want the content to be injected. In your case, your "main" partial would look like:
<main class="vertical-center">
<div class="container-fluid">
<div class="row justify-content-center">
<div class="col-12 col-lg-8 justify-content-center align-items-center">
{{> #partial-block }}
</div>
</div>
</div>
</main>
Then, the page that is calling the "main" partial would do using the block syntax with the content that is to be injected into the partial put inside of block tags:
{{#> main}}
<p>THE RAW HTML CONTENT HERE</p>
{{/main}}
I have created a fiddle for your reference.

Related

Wordpress block theme renders wp:group outside of template part html

I am trying to setup the footer.html template part of my custom block theme for Wordpress.
The template part looks like this:
<footer>
<div class="footer__grid">
<div class="footer__left-space"></div>
<div class="footer_left-block"></div>
<div class="footer__widgets">
<!-- wp:group -->
<div class="wp-block-group"></div>
<!-- /wp:group -->
</div>
<div class="footer__right-section"></div>
</div>
</footer>
I would like to be able to insert blocks in the wp:group area, which should then be rendered inside of the footer__widgets element (see html above). The blocks I have set up for this particular wp:group section look like this:
As you can see in the image, it already gives me a This block has encountered an error and cannot be previewed.
Second, the html gets rendered as follows, outside of footer__widgets:
Main question:
How can I make sure the blocks from wp:group get rendered inside of the div.footer__widgets element?
Side question:
Am I doing wrong and/or misusing Wordpress' new Block Theme functionality, also with regards to the error message in the first image?

Trying to Understand BEM

So I am trying to understand the BEM naming structure. So lets say I have the following html
<div class="banner">
<div class="banner__toprow">
<span class="banner__teamName">
{{team.name}}
</span>
<span class="banner__score">
{{team.score}}
</span>
</div>
<div class="banner__timeouts">
{{team.timeOuts}}
</div>
</div>
Now where I get confused is when you have nested divs, how that works. For example for banner__teamName, I would usually do banner__toprow__teamName. Now maybe I can do this, but does this break BEM?
From the BEM's FAQ
What would be a class name for an element inside another element? .block__el1__el2?
According to BEM method, block structure should be flattened; you do
not need to reflect nested DOM structure of the block. So, the class
names for this case would be:
.block {}
.block__elem1 {}
.block__elem2 {}
.block__elem3 {}
Whereas the DOM representation of the block may be nested:
<div class='block'>
<div class='block__elem1'>
<div class='block__elem2'>
<div class='block__elem3'></div>
</div>
</div>
Besides the fact that the classes look much nicer, it makes the elements be dependent on the block only. So, you can easily move them across the block when providing changes to the interface. The changes of the block DOM structure would not need corresponding changes to the CSS code.
<div class='block'>
<div class='block__elem1'>
<div class='block__elem2'></div>
</div>
<div class='block__elem3'></div>
I would suggest doing it like this:
<div class="banner">
<div class="banner__toprow">
<span class="banner__toprow-teamname">
{{team.name}}
</span>
<span class="banner__score">
{{team.score}}
</span>
</div>
<div class="banner__timeouts">
{{team.timeOuts}}
</div>
</div>
I don't believe it is correct to have two Elements (__) of Modifiers (--). I also try not to use camel case in my classes, that might be a personal preference but I feel it could cause problems with certain frameworks like Angular.

Meteor Expected "div" end tag - Any possibility to bypass this?

I am trying to put the start of a div in a "header" template and the end in a "footer" template.
Unfortunately meteor shows the following during the building process and fails to start:
Your app is crashing. Here's the latest log.
=> Meteor server restarted
Errors prevented startup:
While building the application:
file.html:22: Expected "div" end tag
What is the correct way of implementing divs that start in one template and end in another?
Thanks in advance.
Extend your templating as far as necessary.
Without an example of your code, I can only make an assumption of what you're trying to do.
<body>
<div class="container">
<div id="another-div">
{{> header}}
</div>
{{> footer}}
</div>
</body>
<template name="header">
<nav id="header">
<!-- content -->
</nav>
</template>
<template name="footer">
<footer id="footer">
<!-- content -->
</footer>
</template>
That being said, an example of your code in your post will help specify the answer.

How to render a partial for tag in Ghost?

When a user is viewing a tag on my blog, I'd like to be able to display additional information about that tag. I think it would be nice to have a template that's called for each tag I have.
My tag.hbs
{{!< default}}
<div class="row">
<div class="twelve columns">
<h1 class="archive_title"><span>{{tag.name}}</span></h1>
</div>
</div>
<!-- PLEASE LOOK HERE -->
I would like to render a partial here
<!-- PLEASE LOOK HERE -->
<div class="row {{#if #blog.cover}}margintop{{/if}}">
<div class="small-12 large-9 columns">
{{#foreach posts}}
{{> content-post}}
{{/foreach}}
</div>
<div class="small-12 large-3 columns">
{{> sidebar}}
</div>
</div>
<div class="row">
<div class="small-12 columns">
{{pagination}}
</div>
</div>
To render a partial for the current tag name, I've tried using this
{{> tag.name}}
I get a 500 error
[tag.hbs] The partial tag.name could not be found
Is there a way to include a partial with a dynamic name in handlebars?
Additional question: Ideally, I'd like all of the tag partials in their own directory. Is this possible too?
I am quite new to the Ghost platform, but I have been experimenting quite a bit. I see that you attempted to do this:
{{> tag.name}}
That is not actually calling the name for that particular tag, what you were trying to do there was to actually show the contents of the tag.hbs file itself. The reason I claim this is because when I recently just created a "menu.hbs" for a menu bar for my theme I attempted this:
{{> menu}}
Handlebars quickly realized that I had a "menu.hbs" file and poured the contents of that file onto the spot that I placed "{{>menu}}" in.
Essentially what you are trying to attempt is a "path". If you scroll down to "Handlebars Paths" on the Handlebars webpage you will find exactly the method you were trying to attempt. I hope this at least helped a little.
This works:
{{#has tag="foo"}}
{{> partial-for-foo}}
{{/has}}
{{#has tag="bar"}}
{{> partial-for-bar}}
{{/has}}
See the has helper docs.

Glyphicons will not show up in twitter bootstrap

I can't get glyphicons to show up. Currently, a small square shows up instead of the glyphicon. I've tried moving folders, etc. I didn't do custom bootstrap - I just downloaded the whole thing, so there shouldn't be any errors/anything missing. Here's my code:
<div class="container">
<div class="row">
<div class="col-md-4 col-md-offset-4 text-center">
<h2>Welcome!</h2>
<p>
</p>
</div> <!-- heading div -->
</div> <!-- row -->
<div class="row">
<div class="col-md-2 col-md-offset-2 text-center">
<div class="interests">
<span class="glyphicon glyphicon-camera">
</span>
<h4>People</h4>
</div> <!-- interests divs -->
</div> <!-- column divs -->
</div> <!-- row -->
</div> <!-- container -->
Am I missing something at the top that needs to be included to get the fonts to show up? In the head, I have a link to the bootstrap.min.css - should I be linking to anything else?
I'm guessing its's not showing for 2 possible reasons:
1. You have not installed the fonts
2. You have not added the font folder hence bootstrap cannot load the glyphicons.
fonts----a folder containing fonts
css------a folder containing bootstrap css files.
index.html-----this file should link to the href="css/bootstrap.css" which intern calls the fonts files
You may check the following images (2 images).
Or, you can check those fonts.
https://github.com/twbs/bootstrap/tree/master/dist/fonts
Hope it helps.

Resources