Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Kod PHP w plikach TPL
Forum PHP.pl > Forum > PHP
Tomaan
Witam, czytam po tym forum ale nie znam się kompletnie na programowaniu a chciałbym znaleźć możliwie najłatwiejszy sposób na działanie PHP w plikach TPL.

Znalazłem w sieci coś takiego:

Cytat
Poradnik przedstawia jak w prosty sposób sprawić że każdy kod PHP dodany w main.tpl będzie działał.

Instrukcja

1.Otwieramy plik index.php (Znajduje się w głównym katalogu DLE)
1a.Szukamy linijki
echo $tpl->result['main'];

1b.Podmieniamy ją na następującą
eval (' ?' . '>' . $tpl->result['main'] . '<' . '?php ');

2.To wszystko.Od teraz możemy dodawać kod PHP w pliku main.tpl


Czy możecie napisać co znaczy DLE i co zrobić jeśli nie mam takiej linijki w katalogu głównym?

Mój kod z index.php w najwyższym katalogu to:
  1. <?php
  2.  
  3. require_once "_includes/_bootstrap.php";
  4. BugTracker::clearBug();
  5.  
  6. $timer->start('Smarty');
  7. include (SMARTY_DIR . 'Smarty.class.php');
  8. $smarty = new Smarty();
  9. $smarty->assign_by_ref('link', $LinkGenerator);
  10.  
  11. // recognize template directory
  12. $hash = md5(CONF_SKIN_DEFAULT);
  13. $row = $cache->load('template_object_' . $hash);
  14. if ($row === false)
  15. {
  16. $log->debug("Loading skin info and saving it to cache: " . CONF_SKIN_DEFAULT);
  17. $select = $db->select()
  18. ->from(DB_PREFIX . 'skins', array('id', 'editable'))
  19. ->where('name = ?', CONF_SKIN_DEFAULT);
  20.  
  21. $row = $select->query()->fetch();
  22. if ($row['editable'])
  23. {
  24. $row['cat'] = "_skiny_users";
  25. } else
  26. {
  27. $row['cat'] = "_skiny";
  28. }
  29. $cache->save($row, 'template_object_' . $hash, array('template'));
  30. }
  31.  
  32. Zend_Registry::set("DEFAULT_SKIN_ID", (int)$row['id']);
  33. $cat = $row['cat'];
  34.  
  35. $smarty->assign("skinyDir", $cat);
  36. $smarty->template_dir = $cat . "/" . CONF_SKIN_DEFAULT . "/html";
  37. $smarty->compile_dir = $cat . "/" . CONF_SKIN_DEFAULT . "/cache";
  38. $timer->stop('Smarty');
  39. // check if shop is off
  40. if (isset($_GET['check']) && $_GET["check"] == "true")
  41. {
  42. $session->off = true;
  43. }
  44.  
  45. if (CONF_OFF == 1 and !isset($session->off))
  46. {
  47. $smarty->display("off.tpl");
  48. $log->info("Shop is closed. Exiting...");
  49. }
  50.  
  51. if (!isset($_GET['cat_id']) and !isset($_GET['prod_id']))
  52. {
  53. $session->cat_id = '';
  54. }
  55.  
  56. $timer->start('currency.inc');
  57. include ('currency.inc');
  58. $timer->stop('currency.inc');
  59.  
  60. $timer->start('languages.inc');
  61. include ('languages.inc');
  62. $timer->stop('languages.inc');
  63.  
  64. $timer->start('user_functions.inc');
  65. include ('user_functions.inc');
  66. $timer->stop('user_functions.inc');
  67.  
  68. setCurrency();
  69.  
  70.  
  71. $smarty->assign("lang", $session->lang);
  72.  
  73. // set main_page breadcrumb
  74. Zend_Registry::get('breadcrumbs')->append(MAIN_PAGE, $LinkGenerator->
  75. getIndexLink());
  76.  
  77. if ($session->userObject)
  78. {
  79. if (!$session->userObject->checkNotDeleted())
  80. {
  81. $_GET['logout'] = true;
  82. }
  83. }
  84.  
  85. $total = $cache->load('price_list_count_'.$session->lang);
  86. if($total === false)
  87. {
  88. $total = $db->select()->from(TABLE_PRODUCTS, array(new Zend_Db_Expr('count(*) AS total')))
  89. ->joinInner(TABLE_PRODUCT_TRANSLATIONS, $db->quoteInto('product_id=pid AND lang=?', $session->lang), array())
  90. ->query()->fetch();
  91. $cache->save($total, 'price_list_count_'.$session->lang, array('product'));
  92. }
  93. if($total['total']<=2000)
  94. {
  95. $smarty->assign('show_pdf', 1);
  96. }
  97.  
  98. $timer->start('baner.inc');
  99. include ('baner.inc');
  100. $timer->stop('baner.inc');
  101.  
  102. $timer->start('bestsellers.inc');
  103. include ('bestsellers.inc');
  104. $timer->stop('bestsellers.inc');
  105.  
  106. $timer->start('search.inc');
  107. if (isset($_GET['adv_search']) || isset($_REQUEST['search']))
  108. include ('search.inc');
  109. $timer->stop('search.inc');
  110.  
  111. $timer->start('login.inc');
  112. include ('login.inc');
  113. $timer->stop('login.inc');
  114.  
  115. $timer->start('product_list.inc');
  116. include ('product_list.inc');
  117. $timer->stop('product_list.inc');
  118.  
  119. $timer->start('product.inc');
  120. include ('product.inc');
  121. $timer->stop('product.inc');
  122.  
  123. $timer->start('subsc.inc');
  124. if (isset($_REQUEST['subsc']) || (isset($_GET['action']) and isset($_POST['email'])) ||
  125. isset($_GET['verifyorder']) || isset($_GET['verify_code']))
  126. include ('subsc.inc');
  127. $timer->stop('subsc.inc');
  128.  
  129. $timer->start('panel.inc');
  130. if (($_GET['panel'] == true || isset($_POST["end"])) && isset($session->user))
  131. include ('panel.inc');
  132. $timer->stop('panel.inc');
  133.  
  134. $timer->start('basket.inc');
  135. include ('newbasket.inc');
  136. $timer->stop('basket.inc');
  137.  
  138. $timer->start('change_pass.inc');
  139. if ($_GET['change_pass'] == true)
  140. include ('change_pass.inc');
  141. $timer->stop('change_pass.inc');
  142.  
  143. $timer->start('bottombar.inc');
  144. include ('bottombar.inc');
  145. $timer->stop('bottombar.inc');
  146.  
  147. $timer->start('add_ins.inc');
  148. include ('add_ins.inc');
  149. $timer->stop('add_ins.inc');
  150.  
  151. $timer->start('last_added.inc');
  152. include ('last_added.inc');
  153. $timer->stop('last_added.inc');
  154.  
  155. $timer->start('poll.inc');
  156. include ('poll.inc');
  157. $timer->stop('poll.inc');
  158.  
  159. $timer->start('manufactur.inc');
  160. include ('manufactur.inc');
  161. $timer->stop('manufactur.inc');
  162.  
  163. $timer->start('mail_to_friend.inc');
  164. if (isset($_GET['mail_to_friend']))
  165. include ('mail_to_friend.inc');
  166. $timer->stop('mail_to_friend.inc');
  167.  
  168. $timer->start('box.inc');
  169. include ('box.inc');
  170. $timer->stop('box.inc');
  171.  
  172. $timer->start('contact.inc');
  173. include ('contact.inc');
  174. $timer->stop('contact.inc');
  175.  
  176. $timer->start('podd.inc');
  177. include ('podd.inc');
  178. $timer->stop('podd.inc');
  179.  
  180. $timer->start('payment.inc');
  181. if (isset($_GET['paymentname']))
  182. include ('payment.inc');
  183. $timer->stop('payment.inc');
  184.  
  185. $timer->start('counter.inc');
  186. include ('counter.inc');
  187. $timer->stop('counter.inc');
  188.  
  189. $timer->start('links.inc');
  190. if ($_GET['links'] == 'show' || isset($_GET['link_id']) && ValidId($_GET['link_id']))
  191. include ('links.inc');
  192. $timer->stop('links.inc');
  193.  
  194. $timer->start('address.inc');
  195. if (isset($session->user) && $_GET['changeaddress'] == "true" and ($_GET['type'] ==
  196. 1 or $_GET['type'] == 2))
  197. include ('address.inc');
  198. $timer->stop('address.inc');
  199.  
  200. $timer->start('wishlist.inc');
  201. if ($_POST['wishlist'] == 1 and ValidId($_POST['pid']))
  202. include ('wishlist.inc');
  203. $timer->stop('wishlist.inc');
  204.  
  205. // main.php included only if $_GET is empty
  206. $timer->start('main.inc');
  207. $smartyPage = $smarty->get_template_vars('main_page');
  208. if (empty($smartyPage))
  209. include ('main.inc');
  210. $timer->stop('main.inc');
  211.  
  212. $timer->start('menu.inc');
  213. include ('menu.inc');
  214. $timer->stop('menu.inc');
  215.  
  216. $smarty->assign('description', $description);
  217.  
  218. if (isset($error))
  219. {
  220. $smarty->assign('error', $error);
  221. }
  222.  
  223. if (isset($notify))
  224. {
  225. $smarty->assign('notify', $notify);
  226. }
  227.  
  228. $smarty->assign("mainUrl", $LinkGenerator->getIndexLink());
  229.  
  230. $smarty->assign('color', 'red');
  231. // $db->closeConnection();
  232.  
  233. // assign positioning variables
  234. if (empty($HeadConf[$session->lang]['title']))
  235. {
  236. $adm_title = "";
  237. } else
  238. {
  239. $adm_title = es($HeadConf[$session->lang]['title']);
  240. }
  241. if (isset($add_adm_title))
  242. {
  243. $adm_title .= $add_adm_title;
  244. }
  245. define("ADM_TITLE", $adm_title);
  246. define("ADM_KEYWORDS", es($HeadConf[$session->lang]['keywords']));
  247. define("ADM_DESCRIBTION", es($HeadConf[$session->lang]['description']));
  248.  
  249. if(is_array($breadcrumbs) && $_GET['cat_id']>0)
  250. {
  251. $title = Zend_Registry::get('category_title');
  252. }
  253. else
  254. {
  255. $breadcrumbs = Zend_Registry::get('breadcrumbs');
  256. $length = $breadcrumbs->count()-1;
  257. if($length>0)
  258. {
  259. $crumb = $breadcrumbs->offsetGet($length);
  260. $title = $crumb['name'];
  261. }
  262. else
  263. {
  264. $title = ADM_TITLE;
  265. }
  266. }
  267. $smarty->assign("page_title", $title);
  268.  
  269. // assign breadcrumbs
  270. $bc = Zend_Registry::get('breadcrumbs');
  271. $bc = array();
  272. foreach((array)Zend_Registry::get('breadcrumbs') as $b)
  273. {
  274. $b['name'] = mb_truncate($b['name'], 25, ' ... ', false, true);
  275. $bc[] = $b;
  276. }
  277. $smarty->assign('breadcrumbs', $bc);
  278.  
  279. // -- ----------------------------------------------------------
  280. // zamias t $smarty->display('index.tpl') dajemy:
  281. // -- ----------------------------------------------------------
  282. $timer->start('smarty compiler');
  283. $log->debug("Smarty is compiling the template");
  284. $smarty->display('index.tpl');
  285. /*
  286. $btm_output = $smarty->fetch('index.tpl');
  287. $btm_includ='./_var/btm.tpl';
  288. if(file_exists($btm_includ)){
  289. $btm_dopis=file_get_contents($btm_includ);
  290.  
  291. $btm_c = 0;
  292. $btm_output=preg_replace('/((<\/body>)?(\s)*(<\/html>)?(\s)*)$/',$btm_dopis.'\1', $btm_output, 1 , $btm_c);
  293. if($btm_c==0)$btm_output=$btm_output.$btn_dopis;
  294. }
  295. echo $btm_output;
  296. */
  297. // -- ----------------------------------------------------------
  298.  
  299. $timer->stop('smarty compiler');
  300. $timer->stop();
  301. $log->debug("Process has successfully ended in: " . $timer->get() . " s.");
  302. $log->debug("Time Limit: " . ini_get('max_execution_time') . " s.");
  303. $log->debug("Memory Usage: " . memory_get_usage() . " b");
  304. $log->debug("Memory Peak Usage: " . memory_get_peak_usage() . " b");
  305. $log->debug("Memory Limit: " . ini_get('memory_limit'));
  306. $log->info("Exiting...");
  307. ?>
wookieb
W smarty kod php umieszcza się pomiedzy tagami {php} {/php} Poza tym evali się nie używa i tak to nie działa.
A chyba najlepszym sposobem korzystania z szablonów jest odejście od składni smarty i zastosowanie zwykłego phpa. Z tą tylko różnicą aby stosować odpowiedni jego zapis google -> php alternative syntax . Przykładem którym niestety muszę się posłużyć jest Zend_View (Nie znaczy to, że masz z niego korzystać)
Zyx
Najlepiej by było, gdybyś zajrzał do dokumentacji Smarty'ego:

http://www.smarty.net/manual/en/language.function.php.php
Ociu
Cytat(Zyx @ 11.09.2009, 08:35:13 ) *
Najlepiej by było, gdybyś zajrzał do dokumentacji Smarty'ego:

Dokładnie. Zamykam.
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.