in my PyroCMS website I want to display rock shows of a rock band that will occur in the future. I use a conditional to check whether the date of the blog post (which contains the rock show information) occurs in the future or not. But apparently PyroCMS does not show any blog post that has a date in the future. How can I solve this?
Below the code that I use:
<h2>Upcoming shows</h2>
{{ blog:posts category="shows" order-by="created_on" order-dir="asc" }}
{{ if created_on >= time() }}
<div class="upcoming_show_info">{{title}}</div>
{{ endif }}
{{ /blog:posts }}
Thanks!
If you just need a dirty fix you have to edit posts() function in system/cms/modules/blog/plugin.php. In version 2.1 you can find it on line 51.
...
->where('created_on <=', now())
...
Becomes:
...
->where('created_on >=', now())
...
This way you can also remove that created_on check in your view file as the plugin is going to return only future dated posts. (If you need both "past" and "future" posts, simply delete that line)
Although this is a quite easy way to fix your problem, it will be better if you create a new function inside plugin.php file and use that in your views.
Related
I am really sorry that I bother you again with my question, but this time I am asking about a kind of best practice. I found already various articles/posts about the topic and followed this article: https://makitweb.com/auto-populate-dropdown-with-jquery-ajax-in-laravel/ how to fill a second select based on the choice of a first select. As far as I can see, I have to use javascript and create new route.
My question would be is a new route the best idea because I also can also just call it by itself in the URL bar, eg. URL /getEmployees/1
How can I archive that I still get the required information but at the same time avoid that the URL can be used. Do I have to fetch all required data in the controller itself before I return the view and then filter them?
Thanks
Stephan
Update:
Another approach/try,
What is if I pass directly both tables to my view:
return view ('test')->with(compact('stuff_types'))->with(compact('positions'));
By the way, it really does not matter to select all data in the first instance, both tables will only have a couple of rows.
However, is it now possible to put a filter on the second table in the select view?
{{Form::select('position2', $positions, null, ['class'=>'form-control', 'id'=>"positions2"]) }}
I already tried $positions->where(‘id’, ‘1’), searched the internet for other ways but it always filles the select with the complete table information.
Later on I would love to put a filter with a javascript on the second table, based on the result of the first select, but at first it would be really nice just to see a where condition on it.
Update 2
Ok, another update / question:
Instead of using laravel collectives with
{{Form::select('position2', $positions::pluck('name', 'id'), null, ['class'=>'form-control', 'id'=>"positions2"])}}
I can use html and add an if condition, which I guess can be modified through javascript (will check tomorrow)
<select name="categories" id="categories" class="form-control">
#foreach($positions as $sector)
#if ($sector->id == 2)
<option value="{{ $sector->id }}">{{ $sector->name }}</option>
#endif
#endforeach
</select>
But now I am struggling with my controller. Before I was using this:
$ positions = DB::table('positions')->pluck('name','id');
But this does not seem to work with a standard select so I had to change the pluck to get, but now the larabel collective is not working anymore
$positions = DB::table('positions')->get();
Can anyone explain we why get() works with html, but not with laravel collectives, and vice versa?
I want to order my front page by last modification, following the steps in its documentation it says this code
{{ range .Data.Pages.ByLastmod }}
Should work, but I am getting the posts ordered by date, not lastmod.
Note: Actually I am using paginator, so the code I am using is
{{ range .Paginator.Pages.ByLastmod }}
I've also tried the first snipped, but still ordering by date.
Anyone knows why?
Thanks
First you must set the lastmod field in the front-matter. If you decide not set the field, then the lastmod date falls back to the date field in the front-matter.
If you wish to implement some automation, then you need to initialize your project as a Git repo, set flag enableGitInfo = true in the config file. You may find the details here: gitinfo
Commit your repo changes before every build and you will see the .Lastmod date change accordingly.
I try to render an data from user textarea input saved in my database.
I need to keep the line breaks use nl2br,
and also want to santize to prevent malicious script by using blade {{{ }}}.
But {{{ nl2br($output) }}} wont work, the br tag would also be sanitize.
Please give me some hint, thanks.
For Laravel 4 users:
{{ nl2br(e($message)) }}
e($x) is equivalent to {{{ $x }}}.
Laravel 5 users:
{!! nl2br(e($message)) !!}
e($x) is equivalent to {{ $x }}.
Sawny's answer is a great one that really leverages the power of the Blade syntax well, except I would take it a step further. You can use Blade::extend to create your own Blade # shortcodes so I use the following:
Blade::extend(function($value, $compiler)
{
$pattern = $compiler->createMatcher('nlbr');
return preg_replace($pattern, '$1<?php echo nl2br(e($2)); ?>', $value);
});
Now in your Blade template all have to do is something like this:
<div>#nlbr($sometext)</div>
EDIT: I realized someone coming across this may very well wonder, "Where do I put the Blade::extend function?"
To be honest, it can go in a lot of places (and it depends on if you're using Laravel 4 or 5 as to the 'best' approach).
A simple place to put it is in the routes.php or global.php files as they will get picked up with the least effort. These are however, not the best files to put them in and you would be best off learning to create Laravel Providers.
simply use this remove br no need to write
this function nl2br
it work fine
{!!($mylogin->title) !!}
I have an announcement to make till some date, so I created a time variable as
$announcedate = strtotime('+6 day');
$now = "1382960040";
$smarty->assign('announcedate', $announcedate);
$smarty->assign('now', $now);
And included on tpl file as
{if $now > $announcedate}My Announcement{/if}
I want to be sure before using this and end the announcement specifically after 6th day.
is this the correct way? Or any other guidance?
In short: This will work as you intend it.
However, I'd like to add a few suggestions:
strtotime() returns an integer, and you compare it with a string. This doesn't necessary lead to problems in your use case, but should be improved anyway. A simple update like this changes that:
$announcedate = strtotime('+6 day');
$now = strtotime('now');
Your solution works for your specific task, but isn't very general. If you want to reuse this code for other purposes (other announcements for example) you'll most likely have to adapt the logic. For those reasons, and to keep as much logic away from your display layer smarty, I would check for the announcements only in my php code.
This would look something like this:
php (pseudo code, the idea is, function myAnncouncements() retrieves the announcements you need for a specific timeframe or whatever):
$smarty->assign('announcements', myAnncouncements());
smarty template:
{if isset($announcements)}
{foreach $announcements as $item}
// whatever is needed in here....
{/foreach}
{/if}
I'm having an issue with the Laravel 4 Package Former and the use of checkboxes. I'm trying to Former::populate() a user edit form within a blade template but my checkboxes are always checked.
Here is my current code:
{{ Former::checkbox('is_admin')->text('Is Admin?'); }}
I've tried:
{{ Former::checkbox('is_admin')->text('Is Admin?')->check(false); }}
and
{{ Former::checkbox('is_admin')->text('Is Admin?')->forceValue(false); }}
To no avail.
My database field 'is_admin' is a boolean field. I thought maybe Former didn't like 1s and 0s but it doesn't uncheck my checkboxes even if I set a getter to return false.
Any Ideas?
Thanks
Sounds like a bug with Former.. Keep in mind it looks like you can overrule whatever happened with populate with populateField.