I'm using Haml as a quick way of prototyping layouts. This is not using Rails, Sinatra or any framework.
What I want to do is declare a variable at the top and be able to call it throughout the page, similar to the way I can declare a variable in Sass and use it throughout the code.
!!! 5
%body
/ Declare Variable
- $type = 'Audio'
.container{:id => "page-#{$type}"}
Is this possible?
Drop the $ to avoid declaring a global variable. It should work just fine.
!!! 5
%body
/ Declare Variable
- type = 'Audio'
.container{:id => "page-#{type}"}
Related
After some time I spent staring at the jbake code, I figured out that if I declare my own property in jbake.properties :
...
foo=bar
...
I can reuse that in files that go through a template engine by referencing it as ${config.foo}. I'd like to have this substitution working also on the content lvl, i.e. for files written in asciidoc, living inside the content directory.
Is there any non-trivial way to achieve it? How can I make the templating engine to proccess the result of asciidoc parses engine, or make it running it before the asciidoctor?
I found the answer myself.
To use the property substitution in asciidoc files, add following to the jbake.properties:
...
asciidoctor.attributes.export=true
foo=world
...
and reference the variable in aFile.adoc this way:
Hello {foo}!
So I am currently doing a custom WHMCS template that uses the smarty template system. One of the calls is as follows:
{$customfield.input|replace:'>':'placeholder="Placeholder' >'}
Now this works in that it sets the placeholder with the text Placeholder. What I am trying to achieve is to get the following variable inside where the Placeholder text is:
{$customfield.name}
So I need something like the following:
{$customfield.input|replace:'>':'placeholder="{$customfield.name}" >'}
but that doesn't work.
Is this possible?
Correct way to do it
{$customfield.input|replace:'>':"placeholder='`$customfield.name`' >"}
Drop the braces around the second variable:
{$customfield.input|replace:'>':'placeholder="{$customfield.name|escape:html}" >'}
I am working on a button that switches view with onClick. I wish to store the last/default position in a variable in order to prevent switching to the default view state on each page refresh or navigation.
I read that I can do the following in a php file:
$myVar= -1;
$smarty->assign('myVar', $myVar);
and then use $myVar in the tpl file. But it does not work for me.
The tpl file I am working on is not part of a module and has no .php file in the prestashop root folder.
Can anyone educate me a little on smarty/php and how to create variables and use them to store button's state?
Thanks
Smarty is a PHP template engine for PHP, which facilitates the separation of presentation (XHTML/CSS) from the PrestaShop's core functions/controllers.
A template file (usually with a .tpl extension in PrestaShop) is always called by a PHP controller file (it can be a Front-end core controller or a module controller).
Example: /prestashop/controllers/front/ContactController.php
$this->context->smarty->assign(array(
'contacts' => Contact::getContacts($this->context->language->id),
'message' => html_entity_decode(Tools::getValue('message'))
));
$this->setTemplate(_PS_THEME_DIR_.'contact-form.tpl');
We can see that this file is retrieving information from the database and assigning it to Smarty.
Then, the 'contact-form.tpl' template will display it to the visitors.
The syntax is pretty similar for modules,
example:/prestashop/modules/blocklink/blocklink.php
public function hookLeftColumn($params)
{
$this->smarty->assign('blocklink_links', $this->getLinks());
return $this->display(__FILE__, 'blocklink.tpl');
}
Also, to store values in Smarty variables, you can use the 'assign' function in two ways:
$this->context->smarty->assign('my_smarty_variable_name', $my_value);
or if you have several variables:
$this->context->smarty->assign(array('my_smarty_variable_name1' => $my_value1), ('my_smarty_variable_name2' => $my_value2));
And then in the Smarty template:
The value of my variable is {$my_smarty_variable_name|escape:'htmlall':'UTF-8'}.
The 'escape' modifier is used to avoid XSS security issues.
In order to use variables in your smarty file, you need to use for example :
$this->context->smarty->assign(
array(
'myVar' => $myvar,
'otherVar' => $otherVar
)
);
Then to use it in your tpl file you simply need to use :
<div>my var = {$myVar}</div>
To use a variable in your smarty you need to write it inside {}.
i´m a bit confused about using optional parameters and phpdocs.
I got the following #url statement:
#url GET /pruefvorschrift/:typs
now want to set :typs as optional so i do
function getpruefvorschrift ($typs=null) {...
this isn´t working, value for :typs is never available in $typs.
If i change the above # url rout to use other word e.g. :id it works?
I don´t understand it could anyone help?
For completeness:
I have many functions in this file
get /device.json/{id}
get /device/pruefvorschrift/:typs.json
get /device/serial.json/{serial}
get /device/:id/merkmale.json
Hope one could help,
thx
Inge
The parameter name is not the problem here!
Using optional parameter as part of the URL is strongly discouraged
By setting a default value for $typs you are making it optional
Which means we need to create two routes for the same api method
GET /device/pruefvorschrift/{typs}
And
GET /device/pruefvorschrift
By default restler 3 does not do it, where as restler 2 does it by default
You can add the following to the phpdoc comment to change that behaviour
/**
* #smart-auto-routing false
*/
function getpruefvorschrift ($typs=null) {
But keep in mind this may stand in the way of another route, read further at http://restler3.luracast.com/examples/_006_routing/readme.html and https://github.com/Luracast/Restler/issues/10
I am struggling to find a pattern for storing single use user settings (VAT percentage, tag line. Things that are 1 off by nature) in Rails 3. I need to set up global site settings, which have single instances.
Ideally, I want the answer to be a design pattern, rather than a gem or plugin (unless someone knows a gem or plugin that integrates will with Active Admin)
What do you mean by single use settings? Do you mean things like API keys and environment variables?
If so, then a good practice is to use the ENV hash, and set up ENV variables in the environments file (explained below).
Create a .rb file for each individual gem (or arbitrary entity) that needs settings in your config/initializers/ directory. For example, when using stripe, I created config/initializers/stripe.rb shown below:
Rails.configuration.stripe = {
:publishable_key => ENV['STRIPE_PUBLISHABLE_KEY'],
:secret_key => ENV['STRIPE_SECRET_KEY']
}
Stripe.api_key = Rails.configuration.stripe[:secret_key]
This sets up initial settings within my stripe gem, and pulls the variable values from the ENV hash.
To set variables in the ENV hash, you can do so within the config/environments directory. In that directory, you will have three different files: config/environments/test.rb, config/environments/development.rb, config/environments/production.rb. Setting variables in the ENV hash(as shown below).
AppName::Application.configure do
# Set Stripe API Key
ENV['STRIPE_SECRET_KEY'] = "sk_test_key"
ENV['STRIPE_PUBLISHABLE_KEY'] = "pk_test_key"
...
end
How about a class for storing your key-value pairs?
class Settings < ActiveRecord::Base
attr_accessible :lookup, :value
def self.VAT
return self.find_by_lookup('VAT')
end
end
Then you can do #vat = Settings.VAT.value or similar. The lookup is your internally defined key. Of course the value column will have to be all the same datatype, but you can handle any necessary transformation in the getter methods (or through subclasses).