Not sure if the is the appropriate use of handlebars - I've been digging around the web and haven't come up with much. Anyway, I'm using assemble.io and trying to set up a partial to repeat around my site. I have a moduleResources.hbs in my /partials directory. Inside that I have this code:
<div class="dvp-content-well">
<div class="content-well-inner">
<h4>Related Resources</h3>
<ul class="fa-ul">
<li><i class="fa-li fa fa-file-o"></i>List Item 3</li>
<li><i class="fa-li fa fa-play-circle-o"></i>List Item 1</li>
<li><i class="fa-li fa fa-cog"></i>List Item 2</li>
</ul>
</div>
</div>
That code block could be called one to two times on a page. Basically, what I need to do is change the content of the <h4> and <ul> dynamically. And was hoping I could do so when calling the partial on the page. So like {{> moduleResources relatedResources }}
Like having all the HTML in the partial but changing it based on context like:
<div class="dvp-content-well">
<div class="content-well-inner">
<!-- IF Related -->
<h4>Related Resources</h4>
<ul class="fa-ul">
<li><i class="fa-li fa fa-file-o"></i>List Item 1</li>
<li><i class="fa-li fa fa-play-circle-o"></i>List Item 2</li>
<li><i class="fa-li fa fa-cog"></i>List Item 3</li>
</ul>
<!-- If Mentioned -->
<h4>Resources Mentioned in this Article</h4>
<ul class="fa-ul">
<li><i class="fa-li fa fa-file-o"></i>List Item 1</li>
<li><i class="fa-li fa fa-play-circle-o"></i>List Item 2</li>
<li><i class="fa-li fa fa-cog"></i><a href="#">List Item 3/a></li>
<li><i class="fa-li fa fa-file-o"></i>List Item 4</li>
<li><i class="fa-li fa fa-play-circle-o"></i>List Item 5</li>
<li><i class="fa-li fa fa-cog"></i>List Item 6</li>
</ul>
</div>
</div>
I had originally set YAML variables like resources-title: Related Resources but you can see if I use the module twice and each version needs to have a different title (and ul content) ... what then?
Is that even a reasonable use scenario for handlebars/assemble?
Thanks!
I think what you're thinking is correct. You can pass a different context to the partial and use handlebars templates to populate the html based on the context. You might even want to split it down to smaller pieces:
{{!list-item.hbs}}
<li><i class="fa-li fa fa-{{icon}}"></i>{{text}}</li>
{{!list.hbs}}
<ul class="fa-ul">
{{#each items}}{{> list-item . }}{{/each}}
</ul>
{{!resources.hbs}}
<div class="dvp-content-well">
<div class="content-well-inner">
<h4>{{title}}</h4>
{{> list .}}
</div>
</div>
{{!my-page.hbs}}
{{> resources resources.related}}
{{> resources resources.mentioned}}
# resources.yml
related:
title: Related Resources
items:
-
icon: file-o
url: "#"
text: List Item 1
-
icon: play-circle-o
url: "#"
text: List Item 2
-
icon: fa-cog
url: "#"
text: List Item 3
mentioned:
title: Resources Mentioned in this Article
-
icon: file-o
url: "#"
text: List Item 1
-
icon: play-circle-o
url: "#"
text: List Item 2
-
icon: fa-cog
url: "#"
text: List Item 3
-
icon: file-o
url: "#"
text: List Item 4
-
icon: play-circle-o
url: "#"
text: List Item 5
-
icon: fa-cog
url: "#"
text: List Item 6
Related
I am trying to add a bootstrap menu to my angular 6 project. This is the code
<ul class="nav nav-pills">
<li class="nav-item">
<a class="nav-link active" href="#">User</a>
</li>
<li class="nav-item dropdown">
<a class="nav-link dropdown-toggle" data-toggle="dropdown" href="#">Customer</a>
<div class="dropdown-menu">
<a class="dropdown-item" href="#">OL User</a>
</div>
</li>
</ul>
The issue is, when I click customer link, URL redirect to the localhost:4200/#
and submenu is not opening. below code techniques, I used to project:
app.component.ts
import {HashLocationStrategy, LocationStrategy} from '#angular/common';
app.routing.module.ts
#NgModule({
imports: [RouterModule.forRoot(routes,{useHash: true})],
exports: [RouterModule],
})
Local redirects in Angular are usually done by using routerLink:
From the DOCS
<a [routerLink]="['/user/bob']" [queryParams]="{debug: true}" queryParamsHandling="merge">
link to user component
</a>
More information about Angular routing
You can use href if you want. But the biggest issue is that you haven't associated a component with a route.
Take a look at this example
I have the following array / sub array structure
"filters": {
"types": {
"34": "Ford",
"22": "Jeep",
"18": "Porsche",
},
"locations": [
"Earth",
"Mars",
"Moon",
]
}
and the following handlebars template
{{#if filters}}
{{#each filters}}
<div class="cars">
<ul class="cars__list">
<li class="cars-{{#key}}__title">Filter by {{#key}}:</li>
<li class="cars-{{#key}}__filters">
<ul>
<li class="cars-{{#key}}">View All</li>
{{#each this}}
<li class="cars-{{*want to access filters[key]*}} color={{#key}}">{{this}}</li>
{{/each}}
</li>
</li>
</ul>
</div>
{{/each}}
{{/if}}
I'm having trouble accessing the filters[types] and filters[locations] within the each this loop.
In my CSS I'm using a classes called .cars-type and .cars-location. I want to be able to style each list separately and unfortunately target each li with a class. I want to apply these styles within the each this loop.
I can do it within the filters loop by using {{#key}} but not in the each this loop
I've tried
<li class="cars-{{../filters}}">{{this}}</li>
but this just returns the car type like ford - I want the key ie. '34' in this case
<li class="cars-{{lookup ../filters #index}}">{{this}}</li>
using handlebars helper lookup but again no luck
<li class="cars-{{../this}}">{{this}}</li>
and the above which gives me [object Object]
I've checked out handlebars - is it possible to access parent context in a partial?, handlebars.js: relative paths in partials not working and Lookup helper in handlebars but no luck with any of the solutions
EDIT Here's the HTML output that I want to produce
<div class="cars">
<ul class="cars__list">
<li class="cars-types__title">Filter by types:</li>
<li class="cars-types__filters">
<ul>
<li class="cars-types">View All</li>
<li class="cars-types color-34">Ford</li>
<li class="cars-types color-22">Jeep</li>
<li class="cars-types color-18">Porsche</li>
</ul>
</li>
</ul>
<ul class="cars__list">
<li class="cars-locations__title">Filter by locations:</li>
<li class="cars-locations__filters">
<ul>
<li class="cars-locations">View All</li>
<li class="cars-locations color-0">Earth</li>
<li class="cars-locations color-1">Mars</li>
<li class="cars-locations color-2">Moon</li>
</ul>
</li>
</ul>
</div>
You should reconsider your HTML because a ul cannot be a direct child of another ul. See https://developer.mozilla.org/en/docs/Web/HTML/Element/ul#Usage_context
With that said, we can solve your problem. The Handlebars docs have our answer:
Nested each blocks may access the interation variables via depth based
paths. To access the parent index, for example, {{#../index}} can be
used.
Therefore, your problematic line should look like the following:
<li class="cars-{{#../key}} color-{{#key}}">{{this}}</li>
I am trying to add css class in existing razor dropdownList. Here is my code
var webHelper = EngineContext.Current.Resolve<IWebHelper>();
var currencies = Model.AvailableCurrencies.Select(x => new SelectListItem
{
Text = x.Name,
Value = webHelper.ModifyQueryString(Url.RouteUrl("ChangeCurrency", new { customercurrency = x.Id }), "returnurl=" + HttpUtility.UrlEncode(HttpContext.Current.Request.RawUrl), null),
Selected = x.Id.Equals(Model.CurrentCurrencyId)
});
#Html.DropDownList("customerCurrency", currencies, new { onchange = "setLocation(this.value);" })
I have to make the dropdown list a bootstrap dropdownlist like this...
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" href="#">USD
<i class="fa fa-angle-down pull-right"></i>
</a>
<ul class="dropdown-menu">
<li>
<a>USD</a>
</li>
<li>
<a>BDT</a>
</li>
<li>
<a>EU</a>
</li>
</ul>
</li>
What can i do?
Does this work?
#Html.DropDownList("customerCurrency", currencies, new { onchange = "setLocation(this.value);", #class="dropdown-menu" })
Taking a short look at the Bootstrap documentation for dropdowns, it appears that you need to apply the class dropdown. However, the dropdown rendering of the default Html helper might not correspond to the dropdown rendering required by the Bootstrap dropdown, hence your class does not have the desired effect.
EDIT (following your MVC4 comment):
To mimic the functionality of the Bootstrap dropdown, you could try using this:
<li class="dropdown">
<a class="dropdown-toggle" data-toggle="dropdown" data-hover="dropdown" href="#">USD
<i class="fa fa-angle-down pull-right"></i>
</a>
<ul class="dropdown-menu">
#foreach(var currency in Model.AvailableCurrencies)
{
<li>#currency.Name</li>
}
</ul>
</li>
You could also try to use Html.ActionLink instead of the "static" a element.
For a new application we are using Bootstrap v3.0, which has the navigation menu defined as follows:
<div id="sidebar">
<ul>
<li class="active"><i class="icon-home"></i> <span>Dashboard</span></li>
<li class="submenu">
<i class="icon-beaker"></i> <span>UI Lab</span> <i class="arrow icon-chevron-right"></i>
<ul>
<li>Interface Elements</li>
<li>jQuery UI</li>
<li>Buttons & icons</li>
</ul>
</li>
<li class="submenu">
<i class="icon-th-list"></i> <span>Form elements</span> <i class="arrow icon-chevron-right"></i>
<ul>
<li>Common elements</li>
<li>Validation</li>
<li>Wizard</li>
</ul>
</li>...
This is currently sitting in a shared _Layout.cshtml, and don't currently see a need to move this into its own shared view.
The layout template consists of several static files with <li class="active"> hard coded for the corresponding menu item within that file.
Since I'm building this with MVC4, I would like to know how to dynamically set this, based on the view being displayed.
Not sure best practice on this. You could try something like this:
<li class="#( RouteVariable.CurrentController.Equals("Home") ? "active" : "")">
Or RouteVariables.CurrentAction may be of use.
Suppose I have 2 pages Contact, About.
wp_nav_menu( array( 'show_home'=>true ) )
will display three links as 'Home', 'Contact', 'About'.
Here I would like to add one more custom link as 'Example' and navigate to www.example.com
Excepted result
<div class="menu">
<ul>
<li>
<a title="Home" href="http://www.test.com/">Home</a>
</li>
<li class="page_item page-item-2 current_page_item">
<a title="About" href="http://www.test.com/?page_id=2">About</a>
</li>
<li>
<a title="Home" href="http://www.test.com/?page_id=3">Home</a>
</li>
<li>
<a title="Home" href="http://www.example.com/?page_id=3">Example</a>
</li>
</ul>
</div>
This is using the new built in Menus available under Appearance -> Menus. Above the box of Pages there is a place to add custom links. You would put in "Example" and "www.example.com" and click "Add to Menu".