How to include the query string in Expression Engine pagination links? - expressionengine

I am working on an Expression Engine site and am trying to fix a bug with pagination. A user performs a search which displays paginated results. The search terms are in a query string, so all I need to do is include the query string in the pagination links. However, I don't know how to configure this in Expression Engine. This is all I have to work with:
{paginate}
<div class="results-pager">
Page {current_page} of {total_pages} pages {pagination_links}
</div>
{/paginate}
Can I pass some sort of option to {pagination_links} to include the query string in the links it generates? Or do I need to write this code myself?

I found a way to do it. If you enable PHP code in your templates, you can manually recreate the pagination_links functionality and include the query string in the generated links.
Replace this:
{pagination_links}
With this:
{if total_pages > 1}
<div class="pagination">
{pagination_links}
{first_page}‹ First{/first_page}
{previous_page}‹ Prev{/previous_page}
{page}
{if current_page}
<strong>{pagination_page_number}</strong>
{if:else}
{pagination_page_number}
{/if}
{/page}
{next_page}Next ›{/next_page}
{last_page}Last ›{/last_page}
{/pagination_links}
</div>
{/if}

You might take a look at this free add-on:
http://devot-ee.com/add-ons/better-pagination
Not sure it's the exact fit for what you're doing though, it might allow you to do what you want without turning PHP on.

Related

Multiple conflicting route parameter formats in Razor Pages

I'm making simple crud forms based on the tutorials for Razor Pages MVVM - https://learn.microsoft.com/en-us/aspnet/core/tutorials/razor-pages/?view=aspnetcore-2.1
The issue is the elements on the Index page use different formats for the route parameter and I end up with URL's like /StockIndexMonths/2?StockIndexId=1
Where both /2 and StockIndexId=1 are the same parameter
The select list will use ?StockIndexId=1
The Create New link will use /1, when returning to the Index /1 is used
If I use the select list again I get both /1?StockIndexId=2
Can anyone tell me the preferred way to force the same parameter format to be used? I'm trying to keep Razor Pages doing it's 'magic'
Index.cshtml
#page "{StockIndexId?}"
#model Investments.Pages.StockIndexMonths.IndexModel
#{
ViewData["Title"] = "Index";
}
<h2>Index</h2>
<form>
<select asp-for="StockIndexId" asp-items="Model.StockIndexNameSelect" onchange="this.form.submit();"></select>
</form>
<a asp-page="Create" asp-route-StockIndexId="#Model.StockIndexId">Create New</a>
<table class="table">
...
Alter the form tag so that it uses the POST method:
<form method="post">
Currently, because the method is not specified, GET is used by default, which appends forms values to the URL as query string values. That's why you see what you are seeing.

Expression Engine- Show HTML within entries loop even if no results

New to Expression Engine. I have the following code:
{exp:channel:entries channel="blogposts" orderby="date" url_title="{segment_3}" limit="1"}
<div id="cat_head_section">
<div id="cat_title">
<div id="lower_bubble_heading" class="cat_title"><h5>{title}</h5></div>
</div>
</div>
<div id="category_main">
{if no_results}
<h2>Sorry, the post you were looking for either does not exist or has been deleted.</h2>
{/if}
{url_title}
</div>
{/exp:channel:entries}
When there are no results returned, I don't get any of the HTML returned (other than the heading), which is vital to the structure of the page. How do I solve this?
Thanks.
This answer assumes that the entry call you are making here is the main one on the page. If this call is really just for a sidebar or something then this answer may not apply.
Couple of things...
If you are building urls like EE "expects" you to you shouldn't have to specify which url segment to look for the url title. The 'url_title' parameter is generally useful when you are doing something that EE does not expect, such loading a specific entry other than the one that appears in the last segment.
If you are limiting your result to one entry, there's no need to specify an order.
Maybe the reason you think you need the 'url_title=' parameter is because you haven't taken a good look at 'require_entry='.
So we are down to this:
{exp:channel:entries channel="blogposts" limit="1" require_entry="yes"}
Now on to your question:
Think about why and how someone would end up asking for a page that you can't find.
Did the entry get deleted? Did someone retweet a mal-formed URL?
A. You may say these requests really call out for 404 response.
Serving a true 404 instead of a valid page with a "Sorry..." message ensures that the bad page will not be indexed by search engines...
To handle this you want EE's redirect tag.
{if no_results}
{redirect="404"}
{/if}
You'll want to have a genuinely helpful 404 page if you do this.
B. Alternatively you may say that when an entry can't be found you already have in mind a 'next best place' to sent the visitor -- better than your site's 404 page.
So if your visitor is looking for a blog entry that doesn't exist (say: /blog/entry/bad_page) the best place to send them would be to your blog index page (say: /blog). If you care about search ranking, then to the extent that the inbound link that is breaking had some SEO value, a 301 redirect here will help to confer that SEO value on to the page to which you are redirecting.
{if no_results}
{redirect='blog/' status_code="301"}
{/if}
The only thing that will show up if there are no results is what's in between the {if no_results} "if" statement. If you want more HTML in there, you just have to add it in there (which looks like you are duplicating it but you won't be because only one or the other will show up - results or no results).
Alternately, if you'd rather not duplicate the code you might choose to save yout HTML as a separate embedded template, and then pass the appropriate values to it as variable parameters. If no results, assign apologies to your variable values.
See http://ellislab.com/expressionengine/user-guide/templates/embedding.html

Drupal Custom Templating Security Issues

I'm mainly a Wordpress guy and am trying to learn the ropes of Drupal 7. My question relates to templating best practices and security concerns. I am working with extremely complex designs (yeah designers right!?) so my markup needs to be clean and just right which I have found Drupal makes extremely difficult with the large hierarchy of template files and functions. Basically the workflow I have found that has been working for me is to override the output of specific content types that I need really specialized markup for at the node level.
So for instance : node--custom-content-type.tpl.php
Like I said I am a wordpress guy and am used to being able to run a database query, grab the exact field values that I want and use them however I want. I have been kpr or printing out the $variables array, studying what it contains, and grabbing values directly like so:
$link = $variables['field_link'][0]['url'];
$link_title = $variables['field_link'][0]['title'];
$target = $variables['field_link'][0]['attributes']['target'];
$text = $variables['field_main_text'][0]['safe_value'];
And then echo'ing out and using the variables in the markup exactly as I'd like:
<article class="getstarted-wrapper">
<a id="tocollege" target="<?php print_r($target); ?>" title="<?php print_r($link_title); ?>" href="<?php print_r($link); ?>"><img src="/sites/all/themes/amped/images/visiticon.png" /></a>
<a id="mapcollege" target="_blank" title="View Location In Google Maps" href="<?php echo $maplink; ?>"><img src="/sites/all/themes/amped/images/mapicon.png" /></a>
<div class="getstarted-top" style="background:<?php print_r($bg); ?>;">
<figure>
<img title="<?php print_r($auth_title); ?>" alt="<?php print_r($auth_alt); ?>" src="<?php print_r($auth_img); ?>" />
</figure>
</div><!--getstarted-top-->
<div class="getstarted-bottom">
<p><?php print_r($text); ?></p>
<a target="<?php print_r($target); ?>" title="<?php print_r($link_title); ?>" href="<?php print_r($link); ?>">Get Started</a>
<span>This will take you to <?php print_r($college_name); ?></span>
</div><!--getstarted-bottom-->
</article><!--getstarted-wrapper-->
I am wondering how this process matches up against best practices, what am I doing wrong, what am I doing right, and more importantly what are my security risks and how can I avoid them??
Every time you output a plain text string (ie anything that is not deliberately markup) into an HTML page, you need to escape it.
In plain PHP templates that is typically done with the htmlspecialchars() function. Drupal offers check_plain() as a short-cut, although not a very short one. You can define your own shorter cut to reduce the pain:
function h($s) {
echo htmlspecialchars($s, ENT_QUOTES, 'utf-8');
}
<a id="tocollege" target="<?php h($target); ?>" title="<?php h($link_title); ?>" href="<?php h($link); ?>">...
(I'm not sure what the use of print_r was for—this is traditionally used for producing readable structured-object output for debugging, but that format of output isn't generally what you want in a production web page, and in your example it was only being used for strings, where it makes no difference anyway.)
The correct approach with Drupal is to sanitize user input on output. Since Drupal has multiple modes of output (not just HTML) it's improper to sanitize on input, so when outputting HTML you can use Drupal's check_plain() function as bobince suggests. check_plain is one of several filter functions available for use, see https://drupal.org/node/28984 for more.
If you are overriding the output and accessing theme variables it is correct that the best practice to run check_plain (or other filter functions) yourself. If it's node properties then you can also use the 'safe' properties as described on the link above.

Access Categories for each entry of a specific channel in ExpressionEngine

So, I am still trying to figure out how to use expression engine and I want to create a list of all of the entries in a channel, which includes the corresponding categories. I can create the list as follows:
{exp:channel:entries channel="custom_channel"}
<div style = "display:block;padding:10px;border-bottom:1px solid #999999;">
<span>Title: {title}</span><br>
<span>Date published: {entry_date format="%m/%d/%y"}</span><br>
<span>Access: {access}</span><br>
<span>Type: {type}</span><br>
{!--Here is where I am trying to access the corresponding
categories for each of the entries--}
</div>
{paginate}
<p>Page {current_page} of {total_pages} pages {pagination_links}</p>
{/paginate}
{/exp:channel:entries}
So, what this does currently is it goes through the entries and creates the html as defined for each entry. I've tried to include {categories} and other related tags but have not been successful. I have looked online and have not been able to find anything related. Is it possible to access the corresponding categories for each entry? If so, how? Any help is appreciated. Thanks.
Yes, you can use the categories variable pair. Incidentally, if you're looking to tap into the ExpressionEngine community, post your questions to expressionengine.stackexchange.com

Pagination on Single-Entry view in ExpressionEngine

I am building a website with ExpressionEngine as the CMS. There is a "Work" section that displays all the projects, and a detail view that shows each project individually. It is at this point, on the single-entry view that I would like to have "prev" and "next" pagination. In my tests I have made it work when the URL is "somedomain.com/index.php/work/detail/" but it does not work when the specific entry is part of the URL: "somedomain.com/index.php/work/detail/some_project/"
I've tried putting the pagination code outside of the {exp:weblog:entries} tag as well as within it, but to no avail. This is what my pagination code looks like:
{paginate}
<ul>
{if previous_page}
<li>< previous</li>
{/if}
{if next_page}
<li>next ></li>
{/if}
</ul>
{/paginate}
You are using pagination for lists of entries, what you need is the next / previous entry tags:
http://expressionengine.com/user_guide/modules/channel/entry_linking.html
Hmm. I'm not sure what the issue is here, as I've never used the {pagination} tag in that way. After checking out the docs, I see that the example code for prev/next links inside of the {pagination} tag is wrapped inside of an {exp:comment:entries} loop instead of the normal {exp:channel:entries} loop.
The docs aren't very clear about the scope of this particular feature of the {pagination} tag. You might want to double check that, in your {exp:channel:entries} loop, you haven't included pagination as a value in the disable parameter.
You could also check out the page in EE's user guide about Next/Prev Linking, which details the use of {exp:channel:next_entry} and {exp:channel:prev_entry} tags in place of the {pagination} tag that you've been using. I've used these tags without a hitch, so I definitely recommend trying them if you can't get your method to work.
Best of luck!

Resources