Skip to content

Instantly share code, notes, and snippets.

@razbakov
Created December 9, 2011 17:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save razbakov/1452481 to your computer and use it in GitHub Desktop.
Save razbakov/1452481 to your computer and use it in GitHub Desktop.
Templates
<?php
class SomeClass {
public function somefunction()
{
$orderId = 1; // some order id for example
$order->load($orderId);
$vars['order_id'] = $order->getIncrementId();
$vars['order_date'] = $order->getCreatedAt();
$vars['order_day'] = date('d', strtotime($order->getCreatedAt()));
$vars['order_month'] = date('m', strtotime($order->getCreatedAt()));
$vars['order_year'] = date('Y', strtotime($order->getCreatedAt()));
$vars['prefix'] = $order->getShippingAddress()->getPrefix();
$vars['first_name'] = $order->getShippingAddress()->getFirstname();
$vars['last_name'] = $order->getShippingAddress()->getLastname();
$vars['city'] = $order->getShippingAddress()->getCity();
$vars['street'] = $order->getShippingAddress()->getStreet1();
$vars['house'] = $order->getShippingAddress()->getStreet2();
$vars['zip'] = $order->getShippingAddress()->getPostcode();
$vars['email'] = $order->getShippingAddress()->getEmail();
$vars['country_code'] = $order->getShippingAddress()->getCountryModel()->getIso2Code();
$this->_applyRules($vars, $rules);
$result = $this->_applyTemplate($exportTemplate, $vars);
}
protected function _parseRules(&$string, &$rules)
{
preg_match_all('#{([a-z_]+):([0-9]+)}#is', $string, $matches);
foreach($matches[1] as $id=>$param) {
$rules[$param] = $matches[2][$id];
$string = str_replace($matches[0][$id], '{'.$matches[1][$id].'}', $string);
}
}
protected function _applyRules(&$vars, $rules)
{
foreach($rules as $key => $value) {
if(isset($vars[$key])) {
$vars[$key] = str_pad($vars[$key], $value);
}
}
}
protected function _applyTemplate($template, $vars)
{
$result = $template;
foreach ($vars as $var => $value) {
$result = str_replace(
'{' . $var . '}',
$value,
$result
);
}
return $result;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment