Meteor JS: include dynamic name template in layout - node.js

I have this basic layout. I want to include a dynamic header to be included in the template. +header should be like this +{{get_header_name}}. get_header_name is a helper function. I tried this idea but jade will throw an error. Any ideas how to make it dynamic?
basic.jade
template(name="basicLayout")
#main
header
+header // <--- make this a dynamic using helper (get_header_name)
+search
else
+yield
footer
+footer

If you don't use Iron Router, you can use Template.dynamic.
Define helper that returns session with template name:
Session.set('headerTemplateName', 'defaultHeader');
Template.basicLayout.helpers({
headerTemplate: function() {
return Session.get('headerTemplateName');
}
});
Use that helper in your basicLayout template:
+Template.dynamic template=headerTemplate
Now, when you change value of session headerTemplateName anywhere in the app, your header template will change according to it:
Session.set('headerTemplateName', 'anotherHeader');
If you use Iron Router, check out Layouts and Regions: https://github.com/iron-meteor/iron-router/blob/devel/Guide.md#layouts

Related

Twig function/filter with no input?

I'm using Slim 3 and Slim Twig-View. I want to add a Twig function (or filter, not sure what is the difference?) which generates a random string, and doesn't take any input.
I was able to add a filter like this:
$twig->getEnvironment()->addFilter(
new \Twig_Filter('guid', function(){ return generateGUID(); })
);
But I can't seem to use it without providing some dummy input:
{{ 0|guid }} This will work
{{ guid }} This will not work
How can I use my guid filter/function without providing any input?
A filter always apply on something, it filters something.
What you want is a function, indeed.
The extending Twig page of the documentation is an incredible source of information on that matter.
At first glance, I would even have said you should define a tag for this but the documentation on the tag, explicitly says:
If your tag generates some output, use a function instead.
Source: https://twig.symfony.com/doc/3.x/advanced.html#tags
So indeed, in order to define a function:
Functions are defined in the exact same way as filters, but you need to create an instance of \Twig\TwigFunction:
$twig = new \Twig\Environment($loader);
$function = new \Twig\Twig_Function('function_name', function () {
// ...
});
$twig->addFunction($function);
So more specifically for you:
$container->get('view')->getEnvironment()->addFunction(
new Twig_SimpleFunction('guid', function(){ return generateGUID(); })
);
Will be accessible via:
{{ guid() }}
Other worth reading:
extending twig, in Slim documentation
you can achieve the same with a macro

Property 'locals' does not exist on type 'Response'

I want to use res.locals to pass some data between middlewares but typescript says I can't. :(
I am using Restify by the way. Do I need a plugin?
If not, an alternative solution for passing data between middlewares would be appreciated.
⚠️ If you use typescript, then you get an error, because locals is not part of Interface res.
So, if you want to add your own property, for example in res, then you can make it like this: res['locals'] = { name: 'John doe' }.
📤 Update: Add properties to restify res
To declare locals properties to your restifyResponse, than you can use the code below:
declare module 'restify' {
interface Response{
locals: any
}
}
After you declare your own properties, An example locals in your Restify Response, so, now you can use locals in your restifyresponse.

Jade/Pug - appending to a class name

Hello I have a unique id in an object and I want to append it to a class name. I am trying to do something like the following but it isn't working:
myJadeFile:
.googleChartContainer-#{attendanceAnalytics.uid}
myRoute.js:
res.render('./edu/school_dashboard_elementary', { attendanceAnalytics:attendanceChart });
I suppose I could create a class name in my route and send it as a variable with something like:
var className = '.googleChartContainer-attendanceChart.uid}';
res.render('./edu/school_dashboard_elementary', { attendanceAnalytics:attendanceChart, attendanceClassName:className });
and then in the jade file:
#{attendanceClassName} //- output is .googleChartContainer-someUid?
I was wondering if there was a way to get the first approach to work correctly, or if there is another preferred way.
Thanks!
You have two choices. You can do it the JavaScript way with a string, like:
div(id=attendanceAnalytics.uid, class='googleChartContainer-' + attendanceAnalytics.uid)
or you create an JavaScript object containing keys and values to use them with the typical jade attribute div&attribute(object), like this:
- var attr = {"id": attendanceAnalytics.uid, "class": 'googleChartContainer-' + attendanceAnalytics.uid}
div&attribute(attr)
Take a look into the JadeLang Docs, chapter attributes.

Extend a view or use a mixin in Ember.js

I would like to include standard functionality in several views within an Ember app. The functionality includes things like setting the tagName and classNames of the views to be the same and keeping track of a property for each view.
The question in a nutshell: Should I use a mixin or extend a base view?
The expanded question...
Should one extend a base view to do this? For example:
App.BaseView = Em.View.extend({
tagName: 'section',
classNames: ['page_section', 'blue'],
willInsertElement: function() {
// Some functions called here that set properties
},
});
App.PageOneView = App.BaseView.extend({
// View specific stuff here
});
App.PageTwoView = App.BaseView.extend({
// View specific stuff here
});
... Or, should one use a Mixin to extend the functionality? For example:
App.BaseMixin = Em.Mixin.create({
tagName: 'section',
classNames: ['page_section', 'blue'],
willInsertElement: function() {
// Some functions called here that set properties
},
});
App.PageOneView = Em.View.extend(App.BaseMixin, {
// View specific stuff here
});
App.PageTwoView = Em.View.extend(App.BaseMixin, {
// View specific stuff here
});
I understand that views and mixins are both Ember objects, but does using either of them to extend standard functionality to other objects (e.g. views) affects how the objects and prototypes/instances (if they differ from the object) interact and whether properties get set on the instance of the view or the view object?
If the two examples above differ, would setting the properties on the mixin's init function change anything? For example:
App.BaseMixin = Em.Mixin.create({
tagName: null,
classNames: null,
init: function() {
this.set('tagName', 'section');
// And so forth...
},
});
However, if using a mixin and extending a view have the same affect on the views I am trying to add the standard functionality to (that is, they affect the views' objects and prototypes/instances in the same way), do you see an advantage to using one over the other (whether in terms of efficiency, maintainability, etc)?
Great question,
Short and simple, extend the view.
The hooks/bindings are view specific, so the mixin can't be applied to a controller, route etc, and depending on your team makeup, you don't want to give someone an opportunity to mix in code that doesn't belong.
Extending a class in Ember just makes the base class into a mixin and applies it to your class. https://github.com/emberjs/ember.js/blob/v1.2.0/packages/ember-runtime/lib/system/core_object.js#L488
So it's almost the exact same thing, only your base view makes more sense since it only really applies to views.

Using $helper in CakePHP 2

I have a question about declaring $helper explicitly.
This is sample code from CakePHP Book.
<?php
class PostsController extends AppController {
public $helpers = array('Html', 'Form');
..
}
In my code, I didn't have that declaration at all, but my app is still working, I can save data via my web form, I can also using $this->Html->link().
Do I really need that declaration, any disadvantages if I didn't?
Thanks to all.
The $helpers variable only needs to be declared when you are using a Helper other than 'HTML' and 'Form'. The core helpers 'Html' and 'Form' are loaded by default into the $helpers array, so the declaration is unnecessary if you only intend to use these.
If you want to add a custom helper, or use any other core helper, then you must declare the $helpers array. When you do this, you are overwriting the default helpers array, so you need to make sure to include the defaults again if you still intend to use them.
// Default. You do not need to declare this if you
// only intend to use these helpers.
$helpers = array('HTML', 'Form');
// Add 'CustomHelper' to $helpers array. In this case
// HTML and Form must be declared.
$helpers = array('HTML', 'Form', 'Custom');

Resources