Including php in a template

Post

Posted
Rating:
#3469 (In Topic #687)
Hello,

Is there a way to include PHP inside a template. For example something like this

<?php require_once '/pcw/shortcode.php'; ?>

Thank you

Post

Posted
Rating:
#3470
There's a hidden option you can enable via Commandr:

Code

:set_value('allow_php_in_templates', '1');

However I would not recommend it, as you're granting any theme you install in the future permission to run PHP code on your server.

You are better off writing and including a mini-block.

Post

Posted
Rating:
#3471
I'm gonna try to lay with the commandr first I thin.

I added the line you provided. Is there any special tags php needs to be wrapped into. Straight up is not working.

Post

Posted
Rating:
#3472
I need to learn about this mini-blocks you are talking about. Is there a place I could read about those?

Post

Posted
Rating:
#3473
Did the Commandr line take correctly? (i.e. no errors)
Did you edit your template after putting in the command?

Post

Posted
Rating:
#3474
I just tested and it worked ok. But maybe your PHP could be wrong too. /pcw looks suspicious, because I highly doubt you have the folder on the drive root. Maybe needs to be <?php require_once get_file_base() . '/pcw/shortcode.php'; ?>.
That said, that code would need to be compatible - if it's pulled from some Wordpress addon it's almost certainly not going to work, it would need to be standalone PHP.

Post

Posted
Rating:
#3475
It took it fine. I think I am the one who is messed up here.

I guess the correct question I should have asked. Let's say I am creating a news article. Is there a way to include php in there?

Pretty sure the template will accept php after the command line addition, but that's not what I intended on asking.

Post

Posted
Rating:
#3476
Oh right. So into Comcode. No, that wouldn't work. We recognise PHP tags in pure Tempcode, but not Comcode.

You can easily include it via a block in Comcode.

Put your PHP code in sources_custom/miniblocks/shortcode.php
and include with [block]shortcode[/block]

Post

Posted
Rating:
#3477
Will try to play with it for a little while. Thank you Chtis

Post

Posted
Rating:
#3478
It works like this for me
require_once '../pcw/shortcode.php';

So how do I call what I need into the [block]shortcode[/block] so that my php file is included in there? Is it a file name I have in the folder?

Post

Posted
Rating:
#3479
In my example sources_custom/miniblocks/shortcode.php is a PHP file, creating a block called shortcode. It's just a normal PHP file that you can put code in - either the direct code you want, or a require_once line like you were wanting to use directly.

Post

Posted
Rating:
#3480
Got it, thank you

Post

Posted
Rating:
#3481
Ok,struggling a bit.

So if my needed folder is in the public_html directory along with all composr folders. lets say called pcw.

What path do I use in my block for sources_custom/miniblocks

What do I need to change below?
require_once '../pcw/shortcode.php';

Post

Posted
Rating:
#3482
Probably:
require_once 'pcw/shortcode.php';

Composr sets the current working directory to the root.

Post

Posted
Rating:
#3483
Awesome, got everything to work. Thank you.

Post

Posted
Rating:
#3877
Hello,

I am creating some PHP scripts for my co-workers to make their daily chores easier and now after reading this post I realize I can embed these tools into a COMPOSR and really make it look awesome!

I am wondering though, is a mini-module as talked about here:

Composr Tutorial: Writing mini-modules

Open Source CMS with advanced social, interactive and dynamic features. Fully flexible, themeable and extendible: suitable for building powerful websites.

View



the same as a mini-block you are discussing in this post?

Thanks much

Stevyn

 

Post

Posted
Rating:
#3878
I believe miniblocks are aimed at simple scripts that reside in a single file and minimodules are for more complex addons. You could do it either way, but one way likely makes more sense over the other depending on what you're aiming to achieve.

Post

Posted
Rating:
#3879
Tomorrow I will give it a go and report back

thinking I will try it first as a mini-module as it is pulling in many things to process. Basically it does a GIT clone, then some edits and a GIT push back. and some other items.

geeking out to try it tomorrow when back at the office     :)

 

Post

Posted
Rating:
#3881
Hi,

Mini-blocks and mini-modules are implemented in exactly the same way, i.e. you just code a pure PHP file without any real forced structure but have full access to Composr's API. Just a mini-block is a block and a mini-module is a module (a page, i.e. accessed by URL).

They are stored in different directories, sources_custom/miniblocks vs <zone>/pages/minimodules_custom.
8 guests and 0 members have recently viewed this.