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

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

Related

How to add a class to a postLink form - CakePHP 3.4

Is it possible to add a class to the hidden form created by CakePHP's postLink form helper?
Here's my code
Form->postLink(
' ' . __('Delete'),
['action' => 'delete', $this->fetch('item')],
['confirm' => __('Are you sure you want to delete # {0}?', $this->fetch('item'))
,'escape' => false
,'title' => __('Delete')
,'class' => 'btn btn-danger btn-xs isAction'
]) ?>
Note that I'm not looking to add a class to the link that gets created.
Any ideas are welcome!
There's pretty much only one way currently, and that would be to change the formStart template temporarily, something along the lines of this:
// read current template and set the new one
$formStart = $this->Form->getTemplates('formStart');
$this->Form->setTemplates([
'formStart' => '<form class="hiddenFormClass"{{attrs}}>'
]);
echo $this->Form->postLink(/* ... */);
// set the template back to its previous state
$this->Form->setTemplates([
'formStart' => $formStart
]);
It's also possible to reset the templates to their default state using the resetTemplates() method, however this would reset all possible changes made to any of the templates, so it's probably better to play it safe as shown above.
See also
Cookbook > Controllers > Views > Helpers > Form > Customizing the Templates FormHelper Uses
API \Cake\View\Helper\FormHelper::setTemplates()
API \Cake\View\Helper\FormHelper::getTemplates()
API \Cake\View\Helper\FormHelper::resetTemplates()

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.

TYPO3: Use t3lib_TCEforms in frontend plugin

I would like to use as much standard TYPO3 as possible to create a form to edit single records from tx_mytable.
In pi1 i load the tca for the table:
t3lib_div::loadTCA('tx_mytable');
Now I would like to use standard functions to create my form elements more or less like it is done in the backend...
I found this for the front end but cannot find any working examples:
t3lib_TCEforms_fe.php (that extends the normal t3lib_TCEforms)
Is this the right way to go or is there a better way?
I got something working but not really that nice code in the frontend
Here is a link that telss that TCA is not enough but two new entries in the array is needed
http://www.martin-helmich.de/?p=15
It is itemFormElName and itemFormElValue
// include tceforms_fe (place outside class where pipase is included)
require_once(PATH_t3lib.'class.t3lib_tceforms_fe.php');
// load TCA for table in frontend
t3lib_div::loadTCA('tx_ogcrmdb_tasks');
// init tceforms
$this->tceforms = t3lib_div::makeInstance("t3lib_TCEforms_FE");
$this->tceforms->initDefaultBEMode(); // is needed ??
$this->tceforms->backPath = $GLOBALS['BACK_PATH']; // is empty... may not be needed
//////////REPEAT FOR EACH INPUT FIELD/////////
// start create input fields, here just a single select for responsible
// conf used for tceforms similar to but not exactly like normal TCA
$conftest = array(
'itemFormElName' => $GLOBALS['TCA']['tx_ogcrmdb_tasks']['columns']['responsible']['label'],
'itemFormElValue' => 1,
'fieldConf' => array(
'config' => $GLOBALS['TCA']['tx_ogcrmdb_tasks']['columns']['responsible']['config']
)
);
// create input field
$this->content .= $this->tceforms->getSingleField_SW('','',array(),$conftest);
// wrap in form
$output = '<form action="" name="editform" method="post">';
$output .= $this->content;
$output .= '</form>';
// wrap and return output
return $output;
Still looking for a working example with custem template for input fields.

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.

Using HTML::FormFu, how do you change a field value *after* processing so that it appears modified in Template Toolkit?

For example, if I process a form:
my $form_input = { input_data => '123' };
$form->process($form_input);
Then I want to alter the value of 'input_data':
my $clearme = $form->get_field('input_data');
$clearme->value("546"); # doesn't seem to work
..Before pushing the form object to TT:
template 'index' => { form => $form }; # using Dancer
'input_data' seems to retain it's original value (123). Any hints on what I'm doing wrong, or what I should be doing?
Thanks
After looking at the documentation and doing some testing, I think you want
$form->add_valid(input_data => '546');