Smarty - making/converting a contact form - smarty

I'm trying to implement a contact form into a website I'm making. Nobody recommends to just include the php file into a template.
I don't understand how to make or convert an existing contact form to one working with Smarty.
Any guidelines ?

Well, the purpose of smarty is to seperate your code logic from your design template.
So in your contact form, instead of using echo, you should write the output to smarty variables. I.e:
$smarty->assign('email', $email);
At the end of your contact forms PHP you should then load the smarty template
$smarty->display('index.tpl');
I would advice you to read the smarty best practices page as well. It gives detailed information on how smarty templates should be used (and also why not to include plain PHP).

Related

Are there any disadvantages to not using blade and having the front totally decoupled from the back end in Laravel?

I was at an interview yesterday and the interviewer asked me a few questions about blade and I replied saying that I don't use blade and only use Laravel to build RESTful web services which I then consume using a front end technology such as Angular.
He insisted that blade was very useful and there a few disadvantages to not using blade. I couldn't understand what he was talking about. And neither do I clearly remember any points he made.
I am curious to know are there any disadvantages to not using blade in Laravel?
In my opinion there are a few advantages of AngularJS over blade and vice versa:
Advantages blade:
Laravel has some html helpers and form helpers.
Laravel adds a token to a form for preventing csrf!
When you open a form you can give a route to it. The method and the action are defined by blade based on the given route.
With blade you can create macro's and view composers.
When validating a form you can return back with the input and the errors which is quite easy.
Blade is not realy slow because laravel cache the compiled views.
With blade you can extend and include views which is more DRY(Don't Repeat Yourself)
On paginator objects you can call ->links() which is very easy for pagination although this is also nice to do with AngularJS.
Blade can automatically escape you're data with {{{$var}}}.
If you've to write a multi language application blade and laravel comes with easy methods to do achieve this.
This are in my opinion the advantages of blade. Look at the blade docs for more information and figure out what you like!

Share templates between Laravel and JavaScript

How do I share template information between my PHP backend and JavaScript / AJAX requests?
Times ago I just sent my AJAX requests and had the HTML generated by the server and sent as such. Today I have my AJAX data as JSON but I have no idea how to use the same template (e.g. a users list) at the server side and (for refreshing, filtering etc.) at client side without creating redundant layout code.
Is there a template language with parsers as well for PHP/Laravel and JavaScript?
The Laravel template engine Blade is obviously not usable in JavaScript.
The only sharing template language I found via Google was Mustache, but the parser for Laravel was outdated.
Is there anything else out there and which approach do you use for that?
Your boiled down question:
Is there a template language with parsers as well for PHP/Laravel and
JavaScript?
Laravel and Mustache | server side:
conarwelsh/mustache-l4 is a Mustache.php wrapper for Laravel 4. They seems to keep up very well as opposed to what you tell (I presume you mean michaelenger/mustacheview which is actually a Laravel 3 bundle). I stand corrected if am wrong.
Laravel's Blade doesn't rule out Mustache at all. You just have to create a Mustache partial without using blade.php extension and include it within a regular Blade template using #include (More details here)
Serving Mustache template:
You can even coin any custom Response you need using Response Macros such Response::mustache(...) leveraging Response::make(...) (see here for more details).
Some samples of interest:
Combining Laravel 4 and Backbone.
Sharing Templates Between PHP and JavaScript | PHP but still relevant!
My short answer (Updated):
Don't look elsewhere: Laravel + Mustache + Javascript if a mix of server|client side rendering is part of your requirements.
Get your hands dirty! :)
I had the same issue with Laravel and Angularjs, what I did is that I created a route to return templates http://domain.com/templates/name
This route will View::make('templates'.$name);, this is an AngularJs template that will be filled with data returned by JSON API. Remember to use non conflicting tags I used {{ for Laravel and <% for Angular.
RENDER YOUR TEMPLATE SERVER SIDE! I'm not sure at what point someone decided you needed to send JSON to the front end, but that's not how they do it in Rails. In Rails, they actually render the template server side, then send it back to the front end and your JS appends it to the page (or, actually sends back the JS + the markup to attach). This saves a ton of time and headache, trust me.

Using HTML instead of CodeIgniter helpers in views

In the views in codeigniter, we can write code for forms using codeigniter. For example for an url the code in codeigniter is:
<?php
anchor('site/myfunction','Send');
?>
My question is whether is better write this code with html in the views:
Send
It's an example, but the question is with all HTML helpers for views. CodeIgniter's user guide suggests to use PHP functions rather than code html. The php code requests to the server while html does not. Is better use the CodeIgniter for HTML? I don't know if when I use CodeIgniter's helpers, the framework has contemplated these requests.
I apologize for my english. Thanks for your answer.
The reason you want to use CodeIgniters library is for the ability to quickly modify your HTML elements site-wide with very little work. For instance, let's say you wanted all <a> tags on your site to have a class added called "ajax". Using the anchor helper, you can accomplish this easily.
That said, I don't really foresee many solutions where you will be changing HTML elements site-wide. With semantic HTML, CSS, and Javascript I think you will be perfectly fine without having to use CodeIgniters HTML helpers. Also in my opinion your code will be much more readable. Use HTML.
Regarding performance
When you say "code php does requests to the server while html, no" you're wrong because whenever someone visits your site they are requesting the server. The question here is how much work the PHP engine is doing versus just your normal webserver. In this case, a function call is trivial for PHP and shouldn't be considered performance wise.
Regarding urls
The answer by Pi is focused on the fact that URL resolution in CodeIgniter can be weird, but with proper .htaccess or web.config configurations you should be able to use vanilla hrefs without using CodeIgniter functions.
You should not use
Send
Because it might not work everywhere. But if you use this:
Send
There will be no big difference. Pure HTML is a bit faster but unless you have a high traffic website that does not matter. Using the CI function is nice if you are in a library because you do not want to mix PHP and HTML to much to keep up the Model-View-Controller concept. What you use in a view is a matter of style what you like more.
Personally I think the codeigniter form functions are not very good and I am often use html instead.

Getting started with Phil Sturgeon's CodeIgniter Template library

I'm trying to use Phil Sturgeon's CodeIgniter Template library, but I can't get it to change my pages. I read the documentation, followed the steps there,
Download
Copy to application folder
Create a views/layouts/default view
(Auto)load the library
however, it is not clear as to how the template is actually implemented. I thought that loading the view would apply the template everywhere, but it doesn't seem to work that way. I've also read about it in the CodeIgniter Wiki, but it looked too complicated to be the right answer.
How are you supposed to use Phil Sturgeon's Template with your controllers? (or views?) Am I missing something here?
It does not overload the load->view() methods, that would be bad. You need to update your controllers to use the template's syntax and methods in every instance you want to use it:
http://philsturgeon.co.uk/demos/codeigniter-template/user_guide/
You will use $this->template->build() instead of $this->load->view() in most cases, after constructing your template by defining regions, setting titles, etc.
There should have been a user guide included in your download with examples.

Codeigniter: How can I use codeigniter's default view system along with smarty templates

I don't want to remove smarty templates from controllers. In some actions of controllers, I want to use default view system of codeigniter, while on other actions, I want to keep SMARTY templates as display medium. How can I achieve this?
Thanks in advance
I've been developing a CI Smarty library for Codeigniter. It lets you use default Codeigniter views and lets you render Smarty templates using the Codeigniter template parser as I am extending it.
Here is the repository: https://github.com/Vheissu/Ci-Smarty
As it stands, it works really well. If you have any problems, log an issue on Github and I'll fix it usually within 24 hours. Other solutions are mostly outdated, my library lets you use new Smarty 3 features like template inheritance, etc.
Look in the CodeIgniter Wiki - http://codeigniter.com/wiki/Smarty/
And a thread in the official forum:
http://ellislab.com/forums/viewthread/60050/

Resources