Currently, I'm switching a few of my blade templates to Twig in my Laravel project and I had a macro I was using that extended the HTML helper. This is how it appears in my directory:
app
Bunny
Controllers
Facades
Macros
HTML.php
Models
Views
etc...
In my HTML.php file, I have this:
HTML::macro('npc_messages', function($messages) {
// all my logic is in here
});
I have a few other ones as well. I want to know how to make my Twig templates register that macro so I can just use html_npc_messages( var ); like I can with their html_image, etc. Thank you for any assistance!
I checked out the documentation on the github URL: https://github.com/rcrowe/TwigBridge
But I'm not fully understanding what they are doing there under the "Extensions" section.
Related
I have a Laravel 5.0 project and inside of that I have a foo.blade.php file,in here I want to use some Javascript to get the pathname of the link the user currently in like so
window.location.pathname
Is it possible? Or blade templating allows only php to be written on the file?
First, Javascript runs client side, so whatever you write in <script> tag will be totally ignored by laravel.
But if you want to access path (route) in laravel, you can check this question.
I am new to Laravel 4, and I come from a Zend Framework background. I'd like to create a folder app/forms and keep all my forms there. How can I refer to the form in the controller and within the view.blade files?
By default, the root of the view files folder is app/views so if you create a folder in views like app/views/forms then you may refer the form by it's name from a controller like:
$form = View::make('forms.formfile');
Here, formfile is the name of the file that contains the form and it could be formfile.blade.php and to refer/include the form file from a view you may use #include:
// In a blade view file
#include('forms.form1')
Assume that, form1 is a blade view inside the forms folder and saved as form1.blade.php, you may also use sub-folders inside the forms folder, for example in views/forms/user folder you may keep a view named index.blade.php and use it like:
// From a controller
$userForm = View::make('forms/user/index');
From a view file: (folders are separated by .)
#include('forms.user.index') // file: app/views/forms/user/index.blade.php
You can also nest views in the controller, check the manual for more.
From the standpoint of Laravel, HTML forms (and all presentation related things ) belongs to app/views/ folder. Exceptions are package specific views. For example some commands has their own stubs and views and they are usually stored inside package.
Laravel is very flexibile, and you can move things around and create new folders and namespaces. You just have to tell composer that you are changing Laravel default structure, and dumpautoload. That is if you only want to create new folder with internal reference. If you want something with more scope and visibility you'll have to bind that to container, so that will be visible inside whole application.
You are coming from the Zend world, so I can understand that you want to move some of Zend "flavour" to Laravel. Although is it possible, I would really recommend you to take some time and learn how Laravel works. There are some great ideas and design inside. Of course, Zend has its own quality, but hey - this is Laravel :)
I created a custom module called Drawings. I wanted to give it its own theme layout on the frontend, so I followed the advice I found HERE - I put a layout file titled Drawings.html in my theme.
This had the correct effect on the frontend, but caused other problems and questions that I've been struggling to sort out:
The backend admin section for my module now uses the Drawings.html layout, which breaks its functionality completely. How can I set a specific layout for my module front end, but keep the standard admin backend layout (same backend implementation as in the sample module tutorial)?
I read the following in the Template Library: "When using Public_Controller and Admin_Controller, the layout is already set." Perhaps the solution involves using the set_layout function in these controllers somehow? All my attempts failed so far though.
I feel I am doing something incorrectly because now my module is not modular. For example, after creating the Drawings.html layout file in the theme, a statement like {{ theme:css file="drawings.css"}} in that layout file searches for those resources in my theme. So I have to put those JS and CSS resources in my theme, instead of in my module's CSS and JS folders. How do I keep and access my resources in the module's JS and CSS folders?
I don't know how this is supposed to work.
When you are using front end controller use a layout there using philsturgeon template library like this
function index(){
$this->template->set_layout('drawing')
->build('yourview');
}
This way you can load a custom layout for your application.
I'm using Phil Sturgeon's Template Library in my CI installation. When I try to put view partials' files in a theme folder, they do get called properly, but if I try to echo a $template variable in them, I get an error message saying that $template variable isn't defined. If I place those same view partials in the root views folder, $template is echoed properly.
How can I have view partials inside a theme and pass $template to them at the same time?
I was encountering the same problem. This looks like its a problem with some fancy hacks Phil had to use for compatibility with Modular Extensions.
If you're not using a parser, simply turn off parser_enabled in the template config file. Its the first option. Then it'll work.
If you're using a parser, you'll have to dig into the library. The problem is in the _load_view function, check lines 725-728. Something needs to be done here to get the variables to the parsed template.
As in title: How to add helper to view helpers? so I can use it without loading custom helpers group?
You can use the settings.yml to auto load helpers. Don't forget to add the default ones (Partial, Cache) too or they won't be loaded.
For example:
all:
.settings:
standard_helpers: [Partial, Cache, MyCustomHelper]
http://www.symfonyreference.com/ is a good site for symfony configuration reference.
Quoting the "gentle introduction" to Symfony 1.4
The list of the standard helpers, loaded by default for every
template, is configurable in the settings.yml file.
It will avoid to call use_helper() on all of your templates (if I have correctly understood your question).