Global helper for Zend module-structure - helper

My files look like this:
/Zend
/Application
/Configs
/Modules
/home
/controllers
/views
/login
/controllerS
/forms
/library
/login
/models
/plugins
/views
I would like to create a helper which which can be used by all modules. Kinda a "global helper". Is there a standard way to accomplish this?

What kind of helper? View? Or something like a action helper? If it's just a utility style class (not attached to a specific part of the MVC), just putting it in /library/NAMESPACE works well.
Then just add that namespace to the autoloader in application.ini.
autoloadernamespaces[] = NAMESPACE
And you can then
$helper = new NAMESPACE_Helper();

Related

Pug template inheritance with different paths (and depths)

I am running into the problem that I have a 'main layout' which gets extended by other pug files, defining the layout for specific sites.
Example:
main.pug
Path: /we [we.pug extends main.pug]
However, when I start to have routes like /we/are/nice/because which are just specifications of /we and I would like to use the same main.pug, I run into the problem that by the way NodeJS renders the pug files, the src/href paths of JS and CSS includes are off (eg: https://localhost:3001/we/are/nice/script.js won't be found because it is supposed to be at localhost:3001/script.js)
Is there a simple way to fix this issue, or do I need specific main.pug files for all path-depths ?
Have you tried pointing to absolute paths? So /script.js, instead of script.js, in an link/href attribute. This assumes, of course, that the files are served from a server like NodeJS (not by the filesystem).

How to register a Handlebars partial outside partials folder on Sails.js?

I have the following folder structure on my Sails.js application:
assets/
myModule/
views/
partials/
Where myModule is a Backbone component (although my client is not a full Backbone application)
There's a given Handlebar partial x.handlebars that need to be rendered both via server and client sides.
My struggle starts when views/ is not accessible on client side so there's no way to load it on Backbone module.
Then I have tried to move it to assets/myModule/templates/ which would be accessible but now my main template views/layout.handlebars can't load that partial if I try something like:
{{> ../assets/myModule/templates/x}}
which doesn't work as well (I assume Handlebars use views/ as root level for the layouts).
There's only two possible solutions I'm seeing in that situation:
Duplicate these layouts and use one on each location (not ideal, but works)
Place it on assets/myModules/templates/x and register a Handlebars partial on Sails to let it available for the server-side rendering.
How could I do that second solution? Is that possible to register a global partial the same way we can do it with the helpers function under config/views.js?
My goal is to use Handlebars.registerPartial function on config level, outside a Controller so that partial could be reused by other templates as well.
Duplicating the code is not necessary. There are many different ways you can implement this and depends on the optimization profile you will be looking for.
A simple solution would be to read the hbs file in the api/controller with node's fs, then compile it with Handlebars and pass it to the template like this:
var rawTemplate = fs.readFileSync('assets/hbs/client-side-template.hbs', "utf8");
var processedTemplate = Handlebars.compile(rawTemplate);
return res.render('whiteboard/dualloadview', {
myPartialAsVariable: processedTemplate({})
});
You'll have have to figure out how to handle the context and variables used by the partials. You can also improve on this code snippet to by looking into keeping the file read in memory, and looking into how to register partials with Handlebars instead of using a variable.

Yii module, theme layout include module layout. How to to that?

I need do something like that:
I have theme "panel" in /themes/panel and there layout /themes/panel/views/layouts/main.php with example content
<a>$content</a>
I have module "admin" in /protected/modules/admin and there layout /protected/modules/admin/view/layouts/main.php with example content
<b>$content</b>
And on the end i want
<a><b>view</b></a>
Also in /themes/panel/views/layouts/main.php i need load css file from module.
In other explanation:
I have one template for all panels (header, css files, footer), but each of panels need separate layout (only the middle of content change like menu etc.) and one or more individual css file.
Also some images will be include from main theme, other will be individual for modules.
You can use the renderPartial function for load the parts of layout.
<?= $this->renderPartial('webroot.themes.' . Yii::app()->theme->name . '.views.layouts.' . Yii::app()->layout . '.<PART_OF_LAYOUT>') ?>
I disagree that in many help forums of the Internet, when someone asks abot theming a module, everyone suggests a path alias to the themes folder. I think this is wrong, because it implies modules to be splitted, and modules are supposed to be a black-box that can be used across projects. The advice given in such forums would only be valid if a theme is shared among several modules. If someone wants to "package" a theme inside a module, she can:
-add an init function to the controller of the module
-inside that init, use the class attribute layout and a path alias, like this, supose a module whose id is "Sample":
then you add, to SampleCOntroller.php:
public function init() {
//BELOW: it will use the layouts/main.php inside the module.
$this->layouts = "sample.views.layouts.main";
}
Yo can check about path alias here:
http://www.yiiframework.com/doc/guide/1.1/en/basics.namespace

Layout inheritance in jade

If you don't know what jade is.
I am having problem with the template inheritance system.My file structure is like so
/views/
|-- layout.jade
/products/
|-- index.jade
|-- product.jade
/static/
/stylesheets/
|-- style.css
The problems is that when loading the product page which receives an id as param (localhost:3000/product/:id if not for the /id it would load just fine), although the layout still extends correctly it does not load the stylesheet properly (the path is broken). I am doing half of it right though, in the index page of products the stylesheet loads just fine.
Layout.jade
head
link(rel='stylesheet', href='stylesheets/style.css')
It's probably the relative path in your href. Digging around the express documentation, I'm finding that the most popular approach is to reference the stylesheet from the base of the site like this (notice the / preceding stylesheets):
link(rel='stylesheet','/stylesheets/style.css')
This has the benefit of being easy, and working across routes of multiple depths (/about, /about/me, etc). However, it has the negative of not supporting app directory depth. For example, if you wanted to host your app at: http://yourserver/yourapps/yourapp this would be a problem. I don't know if you care about this or not, most of the examples for express certainly don't :-)
However, if you want to do this the right way, there is one example on the express github site: blog. https://github.com/visionmedia/express/tree/master/examples/blog
The approach here is to use a middleware component to grab the base url, and stuff it in the locals passed down to the layout view. Here's what your HTML would look like:
!!! 5
html
head
title Blog
link(rel='stylesheet', href=base + '/style.css')
body
#container!= body
The important parts to check out if you require this approach are middleware/locals.js, app.js where the middleware component is wired up, and layout.jade where the base href is used.
Happy Coding!

asp.net webpages content block and helper differences

In asp.net webpages framework what is the difference between using a content block versus a helper?
They both seem to be used to output HTML to multiple pages. They both can contain code and both can pass parameters.
Are there other differences? When should you use a helper versus a content block?
More info:
With Content Blocks we create a .cshtml (for example _MakeNote.cshtml) file to hold the content we want to insert into a page. Then we use:
#RenderPage("/Shared/_MakeNote.cshtml")
to insert the content into a page. We can pass parameters to the content block like this:
#RenderPage("/Shared/_MakeNote.cshtml", new { content = "hello from content block" })
It's somewhat like an include file, but I think does not share scope with the parent page.
With Helpers we create a .cshtml page in the App_Code folder (for example MyHelpers.cshtml) and place methods in that page which we want to call. The method looks something like this:
#helper MakeNote(string content) {
<div>#content</div>
}
The helper is called by using:
#MyHelpers.MakeNote("Hello from helper")
There isn't a lot of difference functionally. Helpers need to go into an App_Code folder - unless you download VWD or Visual C# Express and compile a binary - and the App_Code folder doesn't translate well to the MVC framework. Of course, that's only relevant if you want to upgrade to MVC at some point.
I would use a helper for generic functional snippets like your MakeNote. I would use a "content-block" (partial, really) for repeated site-specific sections of a page.

Resources