vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB4 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=252)
-   -   Using vb:raw in templates (https://vborg.vbsupport.ru/showthread.php?t=313961)

Black Snow 08-27-2014 09:24 AM

Using vb:raw in templates
 
Hi, I have create multiple custom pages on my forum. I have a navigation bar on the left of all of said pages. Everytime I add/change a link, I need to go into each and every template to add/change that link.

Is there a way to make a template called "custom_nav", add the links for my nav bar, then call that template from each of the custom pages? If so, can someone help me do it or show me where to start please?

Thanks

kh99 08-27-2014 09:40 AM

You could use a template. You would need php code to render your template and save the results in a variable, then register that variable to any template where you want to display it. You could do that in a plugin, or if your custom pages are php scripts, you could do it in your custom scripts.

How are your custom pages implemented?

cellarius 08-27-2014 10:31 AM

See https://vborg.vbsupport.ru/showthread.php?t=228078

Black Snow 08-27-2014 11:26 AM

Quote:

Originally Posted by kh99 (Post 2512847)
You could use a template. You would need php code to render your template and save the results in a variable, then register that variable to any template where you want to display it. You could do that in a plugin, or if your custom pages are php scripts, you could do it in your custom scripts.

How are your custom pages implemented?

The pages are done using templates. I followed an article on here for creating custom pages using templates.

I have this in my php file.
Code:

<?php

// ####################### SET PHP ENVIRONMENT ###########################
error_reporting(E_ALL & ~E_NOTICE);

// #################### DEFINE IMPORTANT CONSTANTS #######################

define('THIS_SCRIPT', 'info');
define('CSRF_PROTECTION', true); 
// change this depending on your filename

// ################### PRE-CACHE TEMPLATES AND DATA ######################
// get special phrase groups
$phrasegroups = array();

// get special data templates from the datastore
$specialtemplates = array();

// pre-cache templates used by all actions
$globaltemplates = array('siterules');

// pre-cache templates used by specific actions
$actiontemplates = array();

// ######################### REQUIRE BACK-END ############################
// if your page is outside of your normal vb forums directory, you should change directories by uncommenting the next line
// chdir ('/path/to/your/forums');
require_once('./global.php');

// #######################################################################
// ######################## START MAIN SCRIPT ############################
// #######################################################################

$navbits = construct_navbits(array('' => 'Forum'));
$navbar = render_navbar_template($navbits);

// ###### YOUR CUSTOM CODE GOES HERE #####

//Use as http:/site.com/test.php?do=siterules
if ($_REQUEST['do'] == "siterules") {
$pagetitle = 'Site Rules';
$templater = vB_Template::create('siterules');
$templater->register_page_templates();
$templater->register('navbar', $navbar);
$templater->register('pagetitle', $pagetitle);
print_output($templater->render());
exit;
}


?>

I think this is all I need in the PHP file.

So, I have a template called "siterules", using the PHP file above. I now want to add a navigation bar on this page and a further 4 pages so when I add/change a link, it reflects on all pages. How can I use a plugin to pull the template "navigation" and insert it on each of the custom pages?

kh99 08-27-2014 02:14 PM

Well, since you have custom php files you don't need to use a plugin. You can add code to render your custom template before the "main" page template. So just after the "YOUR CUSTOM CODE GOES HERE", add something like:
Code:

$templater = vB_Template::create('custom_nav');
$custom_nav  = $templater->render();


Then lower down, add a "register" line to your template code:
Code:

$templater->register('custom_nav', $custom_nav);
then you should be able to put {vb:raw custom_nav} in your siterules template where you want it to appear.

One last thing, for efficiency: add custom_nav to the (existing) array of templates to cache ($globaltemplates):
Code:

$globaltemplates = array('siterules', 'custom_nav');

Black Snow 08-29-2014 08:26 AM

Thanks. I will try that and report back.

--------------- Added [DATE]1409309336[/DATE] at [TIME]1409309336[/TIME] ---------------

Quote:

Originally Posted by kh99 (Post 2512886)
Well, since you have custom php files you don't need to use a plugin. You can add code to render your custom template before the "main" page template. So just after the "YOUR CUSTOM CODE GOES HERE", add something like:
Code:

$templater = vB_Template::create('custom_nav');
$custom_nav  = $templater->render();


Then lower down, add a "register" line to your template code:
Code:

$templater->register('custom_nav', $custom_nav);
then you should be able to put {vb:raw custom_nav} in your siterules template where you want it to appear.

One last thing, for efficiency: add custom_nav to the (existing) array of templates to cache ($globaltemplates):
Code:

$globaltemplates = array('siterules', 'custom_nav');

Worked a treat! :up: thank you very much.

I have one other question. How do I make a page show if there is no
Code:

if ($_REQUEST['do'] == BLAH
I have my pages now set up:
Code:

if ($_REQUEST['do'] == 1
if ($_REQUEST['do'] == 2
if ($_REQUEST['do'] == 3
if ($_REQUEST['do'] == 4

But how do I get page 1 to show if someone goes to http://site.com/info.php

cellarius 08-29-2014 11:39 AM

As a rule: You never, ever once address URL parameters directly like that. Opens you up for all kinds of vulnerabilities.

Read and implement: https://vborg.vbsupport.ru/showthread.php?t=119372

After that, you will have your parameter in a nice variable, for example: do.

And to your question - if a variable is not defined, you define it in your code. See PHP man isset. Along the lines of
Code:

if (!isset($do))
{
    $do = 1;
}

Or do
Code:

if (!isset($do) OR $do == 1))

Black Snow 08-29-2014 03:43 PM

So I could do this:

Code:

if (!isset($do) OR $_REQUEST['do'] == 1)

cellarius 08-29-2014 07:32 PM

Why would you use $_Request again if you have cleaned the input and assigned it to $do? If you havn't done that, $do will not work. And as I said before: Accessing $_REQUEST directly is evil. Do not do it. Read the link I gave you.

This is really basic PHP 101.

Black Snow 08-30-2014 12:05 PM

Quote:

Originally Posted by cellarius (Post 2513076)
Why would you use $_Request again if you have cleaned the input and assigned it to $do? If you havn't done that, $do will not work. And as I said before: Accessing $_REQUEST directly is evil. Do not do it. Read the link I gave you.

This is really basic PHP 101.

Your link doesn't show how to use $vbulletin->input->clean_array_gpc when requesting a page. Could you show me an example?


All times are GMT. The time now is 06:43 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01208 seconds
  • Memory Usage 1,750KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (12)bbcode_code_printable
  • (3)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)pagenav
  • (1)pagenav_curpage
  • (1)pagenav_pagelink
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (10)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • pagenav_page
  • pagenav_complete
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete