Phalcon 2 output flash with twig template - twig

I'm using twig as the template engine, works good, but I'm having an issue outputing the flash messages there. none of these show anything.
{{ this.flash.output() }}
Only {{ content }} works, but it outputs notice and php warning messages as well.
This is how I set the flash
$di->set('flash', function () {
$flash = new FlashDirect([
//tie in with twitter bootstrap classes
'error' => 'alert alert-danger',
'success' => 'alert alert-success',
'notice' => 'alert alert-info',
'warning' => 'alert alert-warning'
]);
return $flash;
});
This how I pass the messages to flash
$this->flash->error('Please use the link sent to you by email');
Any help would be appreciated.

You output flash messages in Volt like: (without the this)
{{ flash.output() }}
And you output flash messages in PHP like:
<?php echo $this->flash->output(); ?>

Are you using a redirect?
If so you need flashSession.

Related

How do I use req.flash() with nunjucks?

I am using connect-flash to pass the flash message. In my controller, I am using the following code if (!req.user) {
req.flash("error_msg", "User not found !!");
return res.redirect(307, '/');
}
In my view, I tried
{{ error_message }}
{{ req.flash('error_message') }}
{{ req.flash.get('error_message') }}
But none of it works. What's the current syntax to parse the flash message in nunjucks ?
First of all you have to install "express-flash" and put below code in app.js
app.js
const flash = require('express-flash');
In Your Controller put below code when you want to pass message
req.flash('error_msg', "User not found !!");
return res.redirect(307, '/');
Put below code in your view file where you want to display message
<% if (typeof messages.error_msg !='undefined') { %>
<p class="error"><%= messages.error_msg %></p>
<% } %>
For anyone facing the same problem you can access the variable using {{ req.session.flash.error_msg }}
If you are using express js and nunjucks as template this is what I have done
in app.js
const flash = require('express-flash');
app.use(flash());
in Routes/controller code
try {
await User.deleteOne({_id:req.params._id}).exec()
req.flash("success_msg", "User deleted");
return res.redirect('/admin/users')
}
in Layout or view file
{% if messages.success_msg %}
<div class="alert alert-success" role="alert">
{{ messages.success_msg }}
</div>
{% endif %}
With connect-flash package, you cannot directly use req.flash in template engine. You need to add a middleware that adds the flash message to the res.locals which can be accessed in template rendering.
Here is a short code snippet.
// this part should be before all the request resolver.
// adding flash function to response locals
app.use((req,res,next)=>{
res.locals.flash = (arg) => { return req.flash(arg) };
next();
});
// request resolver
app.get("/",(req,res)=>{
req.flash("info","show me");
res.render("index.njk",{});
});
app.listen(8080,()=>{...})
In nunjucks file:
<h3>{{ flash("info") }}</h3>

Generate pagination links on twig view using illuminate pagination

I'm having problem how can I able to generate links on twig view using the illuminate pagination. below is my codes.
Route:
$app->get('/', function ($request, $response) {
$data = Todo::paginate(5);
return $this->view->render($response, 'home.twig', [
'title' => 'Home',
'todolist' => $data,
]);
})->setName('homepage');
View:
{{todolist.links()}}
Error:
Call to a member function make() on null
btw I'm using Slim 3
Try this
Raw statement is to print the output as plain html (not text)
Refer to this link https://twig.sensiolabs.org/doc/2.x/filters/raw.html
{{todolist.links | raw}}
I tried on my slim 3 website and its works perfectly.
My Eloquent ver in the composer as below
"illuminate/database": "^5.2",
"illuminate/pagination": "~5.0"
If you're using Laravel pagination & Blade templates.
Try this in your blade file:
{{ $todolist->links() }}
Laravel docs: Displaying Pagination Results

MailJet-Email Template using conditional statement in BLOCKED ststus

Used mailJet module in node js
// template
Hello {{var:first_name:""}},
Welcome to Mailjet. This is an example of a templating
language message. This message contains variables and
nested loops (for .. endfor)! You could also embed
conditions (if .. else ..).
http://www.mailjet.com"}}">
Here is a link to click!
{% for rock_band in var:rock_bands %}
Title: {{ rock_band.name }}
{% for member in rock_band.members %}
Member name: {{ member }}
{% endfor %}
{% endfor %}
// data
{
"Subject": "test subject",
"MJ-TemplateID": "79501",
"MJ-TemplateLanguage": true,
"Recipients":[{ 'Email':"email here(valid email)"}],
"Vars": {
"first_name": "test name",
"rock_bands" : [{
"name": "test_name",
"members" : ['t','v']
}]
}
};
After successfully send email from node application,i checked MailJet dashboard but it's under BLOCKED status.
but template without conditional statements working fine.
Hope there's solution for it and thanks a ton.
The messages appear with status "blocked" when there is an error in the template. I tested the for loop provided and it worked fine for me so most likely there is another issue in the template syntax. Enable the template error management to receive a report about the error:
https://dev.mailjet.com/guides/#templates-error-management
If you still face troubles finding the error, contact Mailjet support as the issue would be related to the specific template.

ejs 2 custom delimiter

EJS2 can only do character to be use with angle brackets for open/close:
ejs.delimiter = '$';
ejs.render('<$= users.join(" | "); $>', {users: users});
I would like to use {{ }} instead of <% %>, previous version will allow this esj.open = '{{' and esj.close = '}}'.
Any help?
EJS has been updated & changed maintainers, so the main site is found here: https://ejs.co/
and the Github is here: https://github.com/mde/ejs
Unlike previous versions of EJS, you can't use completely custom delimiters now - you can use 'partial' custom delimiters - the first set of brackets aren't optional, but the following character is. To set up a custom delimiter you can do the following:
// app.js
const ejs = require('ejs');
ejs.delimiter = '?';
app.get('/', (req, res) => {
res.render('index', {
myVariable: 'Hey!'
});
});
// index.ejs
<div>
<p>
<?=myVariable ?>
</p>
</div>
// index.html (rendered output)
<div>
<p>
Hey!
</p>
</div>
You can add a complete options object and other things, but hopefully that gets you started if you want to change the default delimiters.
Documentation states that custom delimiters are supported:
https://github.com/tj/ejs#custom-delimiters
This isn't possible without modifying the module's source code.
My solution to using custom delimiters is be doing a string replace on the template
const template = fs.readFileSync(path.join(__dirname, './template.html'), 'utf8').replace('{{', '<%').replace('}}', '%>')
const render = ejs.compile(template)
This allows me to declare my template like this:
{{ if (someVar) { }}
{{= someVar }}
{{ } }}

(Post/Redirect/Get pattern) Laravel layout variables don't work after redirect

I'm using the Post/Redirect/Get (PRG) pattern in my Laravel controllers to prevent duplicate form submission.
It works well when I don't use layouts or when my layouts don't use any variable. The problem is my layout uses a variable named $title. When I load the view and the layout without redirect it works well, the title set in the controller is passed to the layout, but after processing a form and redirecting to the same route which uses the same layout and the same controller method I get a "Undefined variable: title" error coming from my layout file.
Here is my code:
File: app/routes.php
Route::get('contact', array('as' => 'show.contact.form', 'uses' => 'HomeController#showContactForm'));
Route::post('contact', array('as' => 'send.contact.email', 'uses' => 'HomeController#sendContactEmail'));
File: app/controllers/HomeController.php
class HomeController extends BaseController {
protected $layout = 'layouts.master';
public function showContactForm()
{
$this->layout->title = 'Contact form';
$this->layout->content = View::make('contact-form');
}
public function sendContactEmail()
{
$rules = ['email' => 'required|email', 'message' => 'required'];
$input = Input::only(array_keys($rules));
$validator = Validator::make($input, $rules);
if($validator->fails())
return Redirect::back()->withInput($input)->withErrors($validator);
// Code to send email omitted as is not relevant
Redirect::back()->withSuccess('Message sent!');
}
}
File: app/views/layouts/master.blade.php
<!DOCTYPE html>
<html>
<head>
<title>{{{ $title }}}</title>
</head>
<body>
#yield('body')
</body>
</html>
File: app/views/contact-form.blade.php
#section('body')
#if (Session::has('success'))
<div class="success">{{ Session::get('success') }}</div>
#endif
{{
Form::open(['route' => 'send.contact.email']),
Form::email('email', null, ['placeholder' => 'E-mail']),
Form::textarea('message', null, ['placeholder' => 'Message']),
Form::submit(_('Send')),
Form::close()
}}
#stop
I don't understand why after redirecting the next line of code is ignored
$this->layout->title = 'Contact form';
I've tried with Redirect::action('HomeController#sendContactEmail'); or Redirect::route('show.contact.form'); but the result is the same.
The controller in charge of rendering that view is exactly the same before the redirect than after the redirect, and it has no business logic at all, so why it only works on the first case but not in the second?
This
Redirect::back()->withSuccess('Message sent!');
should be
return Redirect::back()->withSuccess('Message sent!');
When layout attribute is set in a controller and method is not returning any response, controller try to render the layout. In your sendContactEmail() method both conditions fulfilled and controller tried to render layout before $title is set.
see callAction() in Illuminate\Routing\Controllers\controller.
http://laravel.com/api/source-class-Illuminate.Routing.Controllers.Controller.html#93-127
Have you tried using
return View::make('contact-form', array('title' => 'Contact Form'));
Instead of interacting with the layout directly?
Redirect::back() creates a 302 using the referer value of the current HTTP request. I would start by comparing the initial form request to the redirect request to see if that yields any clues. You could also try...
Redirect::route('HomeController#showContactForm')->withInput()...
I know it's less dynamic but it will generate the URL rather then rely on the referer value in the HTTP header.

Resources