How to make a page-specific title in Dancer templates? - perl

I have a standard Perl Dancer app, using Template::Toolkit as rendering engine, with two routes:
get '/' => sub {
template 'index';
};
get '/foo' => sub {
template 'foo';
};
My views/templates/main.tt contains the line:
<title><%= title %></title>
I want the value of title var be "My Site" on page '/', and "Foo - My Site" on page '/foo'.
I know I can put these values in the controller file, like this:
template 'index', { title => 'My Site' };
but I want to specify them in the corresponding template files, views/index.tt and views/foo.tt.
How can I do that?
Thanks.

This documentation clearly explains how to configure your application to make available in your layout the variables defined in your templates.
For the title tag, you can save yourself the effort to define the title in each template using the variable template.name. Maybe something like that:
<title>
<% template.name.match('(\w+).tt').0.ucfirst %> - <% settings.sitename %>
</title>

Related

How can I make Perl Dancer display my index page properly when the back button is used?

When I fill out a form, press submit, and get the results page everything works perfectly. When I want to go back an fill out a new form the page is broken. It seems to be an amalgam of the index page and the results page. The only way I can get it to work is to re-start the Dancer web engine. Here is a copy of the pm that handles the routes:
package NNSP;
use Dancer2;
use Template;
our $VERSION = '0.1';
get '/' => sub {
template 'index';
};
post '/' => sub {
set layout => 'result_format';
template 'result';
};
true;
I think it's better do
template 'result', $hashref, {layout => 'result_format'};
instead of set layout => 'result_format';
or you should do
set layout => 'default_layout';
in hook 'before' or 'before_template' as set sets global parameters.

How to add menù section to my WordPress template?

I am pretty new in WordPress blog and I am developing a blog with this template:
http://scorejava.com/wordpress351
As you can see at the top of the page there is a "menù" that only show the page in the site (at this moment: "Home" and "Pagina di esempio").
This menù is showed by the following lines of code into the header.php file:
<?php wp_list_pages('title_li=&depth=1'); ?>
So I think that this is not a true menù but only a list of the statics pages present on my blog.
If, in the administrator dashboard I go to the menù section in the "Position of themes" square say me that: "This theme has no support for menus but it is possible use the personalized menu widget to add every created menu in the sidebar"
So I think that my template have no definied a true menù section on the top (but only a list for the static pages). Can I add a true section where add a true menu? How can I do?
Tnx
Andrea
function dasboard_menu() {
global $menu;
$menu[6] = array( __('Orders'), 'read', 'edit.php?post_type=shop_order', '', 'menu-top menu-top-first menu-icon-orders', 'menu-dashboard', 'none' );
$menu[7] = array( __('Catalogue'), 'read', 'edit.php?post_type=product', '', 'menu-top menu-top-first menu-icon-catalogue', 'menu-dashboard', 'none' );
$menu[8] = array( __('Coupons'), 'read', 'edit.php?post_type=shop_coupon', '', 'menu-top menu-top-first menu-icon-coupon', 'menu-dashboard', 'none' );
}
add_action( 'admin_menu', 'dasboard_sub_menu' );
Your starting point is to register your menus in functions.php. Like this:
register_nav_menus(array(
'main_nav'=>__('Main','mythmeme'),
'footer_nav'=>__('Footer','mythmeme'),)
);
It's all in the codex.
You then just need to call the menu in your header.php (or footer.php):
<nav>
<?php wp_nav_menu(
array('theme_location' => 'main_nav')
); ?>
</nav>
Once registered and called you can you use dashboard > appearance > menu to create and add menus to your theme locations.

PHP Simple HTML DOM Parser function to parse(get anchore tag) from html source

I need to find a way to request the href & value of all <a elements from HTML file.
For example:
Domain Link
Output:
href: 'http://domain.com'
value: 'Domain Link'
Try this code.
$html=file_get_html($url);
foreach ($html->find('a') as $links){
//Get link text
echo $links->innertext;
//Get href value
echo $links->href;
}

How can I use a local (or per view) variable in Sinatra with Haml partials?

I have a Haml partial in Sinatra to handle all of my 'page open' items like meta tags.
I would love to have a variable for page_title in this partial and then set that variable per view.
Something like this in the partial:
%title #page_title
Then in the view, be allowed to do something like:
#page_title = "This is the page title, BOOM!"
I have read a lot of questions/posts, etc. but I don't know how to ask for the solution to what I am trying to do. I'm coming from Rails where our devs usually used content_for but they set all that up. I'm really trying to learn how this works. It seems like I have to define it and use :locals in some way but I haven't figured it out. Thank you in advance for any tips!
You pass variables into Sinatra haml partials like this:
page.haml
!!!
%html{:lang => 'eng'}
%body
= haml :'_header', :locals => {:title => "BOOM!"}
_header.haml
%head
%meta{:charset => 'utf-8'}
%title= locals[:title]
In the case of a page title I just do something like this in my layout btw:
layout.haml
%title= #title || 'hardcoded title default'
Then set the value of #title in routes (with a helper to keep it short).
But if your header is a partial then you can combine the two examples like:
layout.haml
!!!
%html{:lang => 'eng'}
%body
= haml :'_header', :locals => {:title => #title}
_header.haml
%head
%meta{:charset => 'utf-8'}
%title= locals[:title]
app.rb
helpers do
def title(str = nil)
# helper for formatting your title string
if str
str + ' | Site'
else
'Site'
end
end
end
get '/somepage/:thing' do
# declare it in a route
#title = title(params[:thing])
end

Zend Navigation setting language parameter for route doesnt reflect in app

I have a zend xml config like so:
<design>
<navigation>
<frontend>
<company>
<label>Company</label>
<route>sitepage</route>
<pages>
<about>
<label>About us</label>
<route>sitepage</route>
<params>
<page>about-us</page>
<language>en</language>
</params>
</about>
Here is my sitepage route:
resources.router.routes.sitepage.type = Zend_Controller_Router_Route
resources.router.routes.sitepage.route = ":language/page/:page"
resources.router.routes.sitepage.defaults.module ="core"
resources.router.routes.sitepage.defaults.controller = "page"
resources.router.routes.sitepage.defaults.action = "view"
resources.router.routes.sitepage.defaults.page = "home"
resources.router.routes.sitepage.defaults.language = "en"
As you can see, what I do is set the page param within the <params> xml node. I tried adding the <language> parameter thinking it would automatically update to the application language, but it doesnt seem to work that way. My navigation menu just outputs, for example, http://localhost/en/page/about-us when It should be http://localhost/it/page/about-us (given that my application is registered as using the it language). How can I get my navigation to recognize the application language (it) ?
I worked this out a different way...
Here is an example for a main registration page:
routes.register_index.route = #lang/#register
routes.register_index.defaults.controller = register
routes.register_index.defaults.action = index
My navigation.xml is empty but you could use just a label and route.
You may have noticed # instead of : in my routes. This is designed mostly for i18n sites.
In my language folder, I have the regular en.php, etc. AND url-en.php, etc.
Here is what it looks like:
<?php
return array(
'lang' => 'en',
'register' => 'register',
'about' => 'about-us',
);
With this, the same route will be used for /en/register, /fr/inscription, etc. (one for each language file)
In my Bootstrap, I include these with the Rewrite initiation:
protected function _initRewrite()
{
$translator = new Zend_Translate('array', APPLICATION_PATH . '/language/url-fr.php', 'fr');
$translator->addTranslation(APPLICATION_PATH . '/language/url-en.php', 'en');
// Set the current locale for the translator
$locale = Zend_Registry::get('Zend_Locale');
$translator->setLocale($locale);
// Set it as default translator for routes
Zend_Controller_Router_Route::setDefaultTranslator($translator);
$front_controller = Zend_Controller_Front::getInstance();
$router = $front_controller->getRouter();
$config = new Zend_Config_Ini(APPLICATION_PATH . '/configs/routes.ini', APPLICATION_ENV);
$router->addConfig($config, 'routes');
$router->addDefaultRoutes();
}
This basically tells your application to translate #lang by its value from url-lang.php according to the current locale. Locale has to be set before this point in your bootstrap. You can either retrieve it from session, cookie, URL, anything.
Once this is done, you can call your route anywhere and it'll be in the current locale, using data from the special language files. Make sure your nav file has the same names as your routes and you should be fine.