I'm using Kohana Framework and this is actually the first framework that I am using. I just wanted to know how to properly add templates in views. What I am doing right now is.
In controller.
<?php defined('SYSPATH') or die('No direct script access.');
class Controller_Welcome extends Controller_Template {
public $template = 'site';
public function action_index()
{
$this->template->header = View::factory('templates/header');
$this->template->header->title = 'Page name - Welcome';
$this->template->header->description = 'Blah blah blah.';
}
Now inside view I make a file called site.php (the view) and echo the variable $header on the top so it shows the contents of the page, and it's working fine but is it actually the right way to do it? I mean echoing out the header in every single view? I'm sure there must be a more complex or better way to do that. I have also heard that the use of Kohana Templete is discouraged.
Have a look at the Mustache Plugin KOstache for Kohana. IMO the best way to separate your layout from your logic.
Have a look at Kostache
It allows you to do simple things like
<li>{{kostachevariable}}</li>
You just create the view extending Kostache class and that's it.
Once you do that you can just set variables using
$pagetitle="My Title"
$myview-bind('mypagetitle',$pagetitle)
In your template file you will only need
<head>
<title>{{mypagetitle}}</title>
It has tons of other nice features.
Related
I have an html file which is the base,where other html documents extends.Its a static page but i want to have variable in the menu.I don't think it's wise to create a view for it,since i don't intend to let users visit the base alone.So where in my project can I store site-wide dynamic variables that can be called on any page without explicitly stating them in their views.
Thank you in advance.
For user specific variables, use session.
For global constants (not variables!), use settings.py.
For global variables, consider to store it in database so it can be multithreading & multiprocess safe.
I looked around and saw different approaches,but one that doesn't compromise the DRY philosophy the most for me is registering a tag in your project then input it in the base template.Its neater See here https://stackoverflow.com/a/21062774/6629594 for an example
Storage can take any number of places, I put mine in a stats model in the db so you get all the goodness of that (and make it easy to access in views).
I then have a context processor written as so:
#context_processors.py:
def my_custom_context_processor(request):
return {'custom_context_variable1':'foo','custom_context_variable2':'bar'}
Add this to your context processors in settings.py:
TEMPLATE_CONTEXT_PROCESSORS = (
...
"my_app.context_processors.ny_custom_context_processor",
)
Provided you use render() to render your templates you can then you can just use:
{{ custom_context_variable1 }}
to return 'foo' in your template. Obviously returning strings is for example only, you can use anything you like so long as your context processor returns a dict.
you can also try using php pages.
Then acces the variable on each page with an include 'file containing the var.php' on every page.
None of this will be visible in the source html as it is only processed on the server side.
If you you would like to try this, mail me and I will send you some sample code.
What is an alternative solution to trying to use dynamic includes using express + jade? Here is what I'm trying to do with the following setup:
routes
index.js
widgets.js
views
widgets
widget1.jade
index.jade
widget.jade
app.js
In app.js, I specify the following route:
app.get('/widgets/:widget', widgets.widgets);
In my widgets route, widgets.js I have:
exports.widgets = function(req, res){res.render('widgets/' + req.params.widget);};
This gives me the ability to view a widget on its own by browsing to /widgets/widget1. This works great. I can also include a widget in another view like this
include widgets/widget1
This works great too. However, I now want to add widget2 and I want to sometimes show widget1 and sometimes show widget2 within another page. I had hoped to pass a variableWidgetName into the view and then reference it like this:
include widgets/#{variableWidgetName}
This fails. When I try to access page, I get a 500 error stating that no such file or directory views\widgets#{variableWidgetName}.jade exists. Clearly I cannot reference variables for includes.
What other options do I have. If this is a limitation in Jade, is there a different approach that I should be using? I've thought of loading the widget via ajax but hoped for a better solution using jade or express.
Does anyone know of a better way to do this?
Try using partial instead of include.
Here is what works for me (I use harpjs.com with jade, which uses "jade": "0.35.0"):
- var name = "something"
!= partial('./'+name+'/hello.md')`
How I can do to get the template Handlebars from file, like this:
App.ApplicationView = Ember.View.extend({templateName: 'templates/myTemplate1.handlebars'});
not from script:
< script type="text/x-handlebars" data-template-name="myTemplate1">
Update
I Will implement Ember-Brunch handlebars template pre-compiling
If you intend on assembling your finished application you can use something like rake-pipeline. This, of course, implies that you also need to use rake-pipeline during development.
If you do not want to assemble your templates, you can call Ember.TEMPLATES['templateName'] = Ember.Handlebars.compile('template content goes here') in your app. You can then separate these templates out to different files, which you will include in your index.html file.
Other than that I do not think there are other options. You could fetch your templates via an AJAX call and feed them into Ember.Handlebars.compile, but then you risk your templates being available too late in the application's lifecycle. Another option is to generate this on-the-fly on the server that delivers your Ember app, but you then most likely have to build-your-own solution.
Refer to the following for an application that uses the Ember.TEMPLATES[''] option: https://github.com/joachimhs/haagen-software.no/tree/master/app/
It is a little cumbersome, but you do get used to it...
There really isn't that many great options for this sort of functionality, and its not a whole lot Ember.js can do about it I'm afraid. .
I believe requirejs + require-handlebars-plugin does exactly what you need. You will have to convert your js application to use requirejs though, but that shouldn't be hard.
More info about requirejs: http://requirejs.org/
require-handlebars-plugin: https://github.com/SlexAxton/require-handlebars-plugin
In the old Play! v1 template loading was very simple. But now I'm at a loss to figure out how I can use Scala templates in a similar fashion to the method below:
val template = TemplateLoader.load(templateName)
val body = template.render(templateBinding)
The use case above is rendering a template to be used for an email in Scala.
The new Scala API has a similar class for working with templates http://www.playframework.org/documentation/api/2.0/scala/index.html#play.api.templates.Html but how would you load the template? Should I just bite the bullet and import Groovy templates? Thanks!
Templates are now compiled java classes. you don't really need to "load" them.
Looks like what you are trying to do is a tag. In which case I would recommend reading this page: http://www.playframework.org/documentation/2.0/ScalaTemplates
Each template is a function and can be easily called with html.Mails.emailtemplate(tags) and use the render() method to build the template. If you only need the text or body of the template, you can also use a syntax like html.Mails.emailtemplate(tags).body.
A particular use case related to the question can be seen in this mailer class for Play! in Gist: https://gist.github.com/2210788
I'm trying out ASP.NET MVC Framework and would like to create an ajax helper method. Basically this helper would act like ActionLink but without encoding its link text. It is rather trivial to write as an HtmlHelper, you simply have to write your own version of GenerateLinkInternal. This isn't working for AjaxHelpers though, as the ajax version of GenerateLink is indirectly calling ToJavascriptString (through GenerateAjaxScript) which is internal, thus cannot be called outside the MVC assembly. I sure can rewrite the whole thing, but it seems way overkill, is there a better way?
Ultimately, I'd like to make this helper act like BeginForm to make the link surround a block of HTML. I've not looked at it yet, but I assume that it uses ToJavascriptString too. I've searched the web and, looking through the MVC source code, I begin to wonder if I'm completely on the wrong track.
Thanks
Update: The more I look at this problem, the more I think that there's simply no solution. Whoever wrote the MVC Framework didn't think about helping people write their own helpers!
Update: I've ended up writing an helper that pretty much duplicate AjaxOptions functionality.
You could probably do this a lot easier by writing your own helper from scratch (i.e. don't make calls to any of the Html.ActionLink()/Ajax.ActionLink() methods) simply by using Url.Action() instead.
For example, it's pretty trivial to do this:
public static string NonEncodedUrl(this HtmlHelper helper,
string linkAction, string text)
{
// Get a new UrlHelper instance in the current context
var url = new UrlHelper(helper.ViewContext.RequestContext);
return String.Format("{1}", url.Action(linkAction), text);
}
You can of course extend this with overloads and extra parameters to suit your own needs.