Call smarty function to another function - smarty

So I am using slim framework together with smarty and I don't want to repeat these lines of code:
require 'vendor/autoload.php';
require 'class.db.php';
\Slim\Slim::registerAutoloader();
$app = new \Slim\Slim();
$app->get('/', 'viewBooks');
$app->run();
function viewBooks()
{
//Dont want to repeat this
require_once('smarty/libs/Smarty.class.php');
$temp = new SmartyBC();
$temp->template_dir = 'views';
$temp->compile_dir = 'tmp';
//Dont want to repeat this end
$db = new db();
$data = $db->select("books");
$temp->assign('book', $data);
$temp->display('index.tpl');
$db = null;
}
As you can see I will have more function and will always include those lines. How can I transfer it to a function and just call it in my viewBooks function?

You could create a hook for this:
<?php
$app->hook('slim.before.dispatch', function() use ($app) {
//Your repetitive code
require_once('smarty/libs/Smarty.class.php');
$temp = new SmartyBC();
$temp->template_dir = 'views';
$temp->compile_dir = 'tmp';
//Inject your $temp variable in your $app
$app->temp = $temp;
});
function viewBooks() use ($app){
$db = new db();
$data = $db->select("books");
//Use your injected variable
$app->temp->assign('book', $data);
$app->temp->display('index.tpl');
$db = null;
}

Related

joomla - router change url when getting the name of product

I have build my own component in joomla and client wants now a friendly urls f.e
website.com/someplace/{product-id}-{product-name}. So i Build my own router like this.
function componentBuildRoute(&$query)
{
$segments = [];
if (isset($query['view'])) {
$segments[] = "szkolenie";
unset($query['view']);
}
if (isset($query['product_id'])) {
$productName = JFilterOutput::stringURLSafe(strtolower(getProductName($query['product_id'])));
$newName = $query['product_id'] . '-' . $productName;
$segments[] = $newName;
unset($query['product_id']);
}
return $segments;
}
and parse route function
function componentParseRoute($segments)
{
$app = JFactory::getApplication();
$menu = $app->getMenu();
$item =& $menu->getActive();
$count = count($segments);
switch ($item->query['view']) {
case 'catalogue' : {
$view = 'training';
$id = $segments[1];
}
break;
}
$data = [
'view' => $view,
'product_id' => $id
];
return $data;
}
While on the end of buildroute function segments are ok I have exactly what I want that on the beginning of parse route I have something like
website.com/szkolenie/1-krakow <-- I dont know wtf is this krakow( I know it is city i Poland) but still where is it get from ? The getProductName function implementation is
function getProductName($productId)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('#__component_training.id as id, #__component_product' . name)
->from($db->quoteName('#__component_training'))
->where('#__s4edu_product.product_id = ' . $productId)
->leftJoin('#__component_product ON
#__component_training.product_id=#__component_product.product_id');
$training = $db->loadObject();
return trim($training->name);
}
So taking all this into consideration I think that something is happening between the buildRoute and parseRoute, something what filters the $segment[1] variable, but how to disable that and why is it happening ?
P.S
Please do not send me to https://docs.joomla.org/Joomla_Routes_%26_SEF
I already know all the tutorials on joomla website which contains anything with sef.
P.S.S
It is built on joomla 3.7.0
You do not have a product named "krakow" ?
If not you can try to remove the $productName from the build function, just to check if this "krakow" is added automaticaly or it's from the getProductName() function.
Also i noticed that you have an error i guess in the function getProductName()
->where('#__s4edu_product.product_id = ' . $productId)
It's should be
->where('#__component_product.product_id = ' . $productId)

CodeIgniter: How to pass variables to a model while loading

In CI, I have a model...
<?php
class User_crud extends CI_Model {
var $base_url;
var $category;
var $brand;
var $filter;
var $limit;
var $page_number;
public function __construct($category, $brand, $filter, $limit, $page_number) {
$this->base_url = base_url();
$this->category = $category;
$this->brand = $brand;
$this->filter = $filter;
$this->limit = $limit;
$this->page_number = $page_number;
}
public function get_categories() {
// output
$output = "";
// query
$this->db->select("name");
$this->db->from("categories");
$query = $this->db->get();
// zero
if ($query->num_rows() < 1) {
$output .= "No results found";
return $output;
}
// result
$output .= "<li><a class=\"name\">Categories</a></li>\n";
foreach ($query->result_array as $row) {
$output = "<li>{$row['name']}</li>\n";
}
return $output;
}
while I am calling this in my controller...
<?php
class Pages extends CI_Controller {
// home page
public function home() {
}
// products page
public function products($category = "cell phones", $brand = "all", $filter = "latest") {
// loading
$this->load->model("user_crud");
//
}
Now, How can I pass the $category, $brand and $filter variables to the user_crud model while loading/instantiation?
You shouldn't be using your model like this, just pass the items you need for the functions you require:
$this->load->model("user_crud");
$data['categories'] = $this->user_crud->get_categories($id, $category, $etc);
I would suggest (after seeing your code) that you study the fantastic codeigniter userguide as it has really good examples, and you just went a totally different way (treating model like an object). Its more simple sticking to how it was designed vs what you are doing.
You can not. A better idea would be to setup some setters in your model class along with some private vars and set them after loading the model.
if you return $this from the setters you can even chain them together like $this->your_model->set_var1('test')->set_var2('test2');

calling function from same controller not passing $data

I am building an HTML mobile app using Jquery Mobile. Its amazing so far.
However, In building my dynamnic template, I ran into a bit of a snag.
Heres my controller:
function index()
{
$data['page'] = "home";
$page['head'] = $this->load->view('template/head',$data,TRUE);
$page['header'] = $this->load->view('template/header', $data, TRUE);
$page['footer'] = $this->load->view('template/footer', $data, TRUE);
$page['nav'] = $this->nav($data['page']);
$this->load->view("pages/home", $page);
}
function nav($page)
{
$data['page'] = $page;
$page['header'] = $this->load->view('template/header', $data, TRUE);
$page['footer'] = $this->load->view('template/footer', $data, TRUE);
return $this->load->view('template/nav',$page,TRUE);
}
Regardless as to why I have it set up this way, any reason why, within the function nav($page) the view would return Undefined variable: header and Undefined variable: footer errors?
It's because you're trying to overwrite a string based variable with an array index.
You're passing in $page to the method, but then trying to create an array called $page to pass to the view. You need to rename the array to pass to the view:
function nav($page)
{
$data['page'] = $page;
// you should rename the array to pass to the view (from $page to $my_page)
$my_page['header'] = $this->load->view('template/header', $data, TRUE);
$my_page['footer'] = $this->load->view('template/footer', $data, TRUE);
return $this->load->view('template/nav',$my_page,TRUE);
}

joomla loadformdata

how to show data from 3 tables in one view, because using JTable i can show data only bind to that JTable, please help me with this one.
my code so far(not working) in models:
public function getEntireProject(){
$item_id = $this->getItem()->id;
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__project_part_1 AS a');
$query->leftJoin('#__project_part_2 AS u ON a.uuid = u.uuid');
$query->leftJoin('#__project_part_3 AS y ON a.uuid = y.uuid');
$query->where('a.id = '. (int) $item_id);
$db->setQuery($query);
return $db->loadResult();
}
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_web_projects.edit.webproject.data', array());
if (empty($data)) {
$data = $this->getEntireProject();
}
return $data;
}
try to overwrite getItem function.This will also be helpful if you are calling get('Item') in view. -
public function getItem($pk = null){
if ($item = parent::getItem($pk)) {
$db =& JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('*');
$query->from('#__project_part_1 AS a');
$query->leftJoin('#__project_part_2 AS u ON a.uuid = u.uuid');
$query->leftJoin('#__project_part_3 AS y ON a.uuid = y.uuid');
$query->where('a.id = '. (int) $item->id);
$db->setQuery($query);
$item = $db->loadAssoc();
}
return $item;
}
protected function loadFormData()
{
// Check the session for previously entered form data.
$data = JFactory::getApplication()->getUserState('com_web_projects.edit.webproject.data', array());
if (empty($data)) {
$data = $this->getItem();
}
return $data;
}
For Multi-Row Results use loadRowList(), loadAssocList(), loadAssocList($key), loadObjectList(), loadObjectList('key'). $db->loadResult() only load one result. Read more.
If I understand your question right this should fix your problem. If you not please ask.

CakePHP 1.3 running shell with cron - passing data to email template

I'm running a shell script via a cron job. It correctly sends an email. However I want to be able to pass data to the email template from the database which I seem unable to do.
Here is the shell
App::import('Core', 'Controller');
App::import('Component', 'Email');
class ExampleShell extends Shell {
var $uses = array('User');
function main() {
$users = $this->User->find('all');
$this->Controller =& new Controller();
$this->Email =& new EmailComponent(null);
$this->Email->initialize($this->Controller);
$this->Email->reset();
$this->Email->to = 'xx<xx#xx.com>';
$this->Email->subject = "Subject";
$this->Email->template = 'example';
$this->Email->sendAs = "both";
$this->Controller->set('users', $users);
$this->Email->send();
}
}
The variable $users does not seem to be available in the example.ctp file? How can I pass data from the shell script to the template please?
I managed to do it in following way
class ExampleShell extends Shell {
var $uses = array('User');
var $Controller = null;
function __construct(&$dispatch) {
App::import('Core', 'Controller');
App::import('Controller', 'App');
$this->Controller = & new Controller();
App::import('Component', 'Email');
$this->Email = new EmailComponent();
$this->Email->initialize($this->Controller);
parent::__construct($dispatch);
}
function main() {
$users = $this->User->find('all');
$this->Controller =& new Controller();
$this->Email =& new EmailComponent(null);
$this->Email->initialize($this->Controller);
$this->Email->reset();
$this->Email->to = 'xx<xx#xx.com>';
$this->Email->subject = "Subject";
$this->Email->sendAs = "both";
$this->Controller->set('users', $users);
$this->Email->send(null, 'template_1');
}
}
I hope it helps someone.
Make sure template is located where it should be app/views/elements/email/html/template_1.ctp and app/views/elements/email/text/template_1.ctp for text version. You should create layouts too in app/views/layouts/email/html/default.ctp and app/views/layouts/email/text/default.ctp

Resources