Can I include php file in .tpl? - php

Can I include a php file to tpl?
All I need is data from the database. I tried with required('path/file.php') but without success.
I already have controller for that. I need to include controller/file, class/php extension anything just need in tpl. I use OpenCart version 2.3.

if you need data from the database, in MVC a model should be used, and then let the controller add it's data to the $data array, which the is available to the template.
this shows how OpenCart actually works (source):
now tell me how your attempt to change a template (view) fits into there (notice the vertical lines).
just load the model into the controller and add results to $data, then it's available in the template.

You should not directly mix templates with custom PHP code, even if in some template engines it's possible.
You should rather look into means of providing your PHP logic through functions, filters or macros. This way you make your PHP code available in templates.
If OpenCart uses twig, you could look into how to do such in the documentation about extending twig here.

Related

What is difference between core view and template views in PHP?

I am little bit confused about normal view and MVC view. What is the difference between both view and why we use blade in framework?
Blade is a templating engine. That means it has some special features that are helpful to you as a developer, such as easily extending other templates or handling php loops in a clean, readable way. You can find an excellent summary of them in the docs: https://laravel.com/docs/5.2/blade
Blade files typically end in .blade.php but there's nothing that says you can't use regular .php files - or another templating engine, if you pull it in through composer. Like most features of Laravel, Blade is entirely optional. But of course: if you don't use Blade, you won't get the features of Blade.
A view in Laravel terms is simply a named template file, Blade or not. You can, for instance, use the view() shortcut function to generate a response to send back to the user, from any template file in your application.

Pass Variables to Included PHP file inside a Joomla Article

I'm new to Joomla, but not a new programmer. I've written several applications in PHP that I want to include inside Joomla articles. Simple enough:
<?php include 'file.php'; ?>
The issue is that inside the PHP files I have a bunch of code gathering and creating variables that I need to POST and retrieve. I can get those POST variables inside the Article, but I can't pass them back to the included PHP file.
I've even coded the included PHP files to access the Joomla framework hoping to retrieve Joomla user id for example. This won't run inside the Article either and returns empty. However, if I run the PHP file on its own outside of the Article, I can access all POST data (obviously) and also the Joomla JFactory data. So it runs fine, until it's placed as an included file inside an Article.
The only way I've been able to pass something to the included PHP file is using $_GET url variables like this:
<?php include 'file.php?data=something'; ?>
However, this simply isn't practical as I have too many variables to pass like this. Normally, included PHP files run as part of the parent script and have access to all variables. How can I accomplish this in Joomla??
Much appreciated!
It is not recommended to include php into articles. Try instead one of these approaches:
Load your code in the index.php - file in your template: /templates/yourtemplate/index.php. This file is called every time your page is called.
Make a template override of the component where you want you external php file to be loaded. If this is in an article, you copy /components/com_content/views/article/tmpl/default.php (and possibly also other files there) to /templates/yourtemplate/html/com_content/article. Then add your include-statement here. (info) This file will be loaded each time you view a single article, but you can have further logic to only run it if (whatever)...
Use Jobin Jose's approach if you want to load you php-file inside content in an article. (info)
Some other approach writing a plugin
...or a component
I would say probably the easiest method is 2. (or 1.), but it all depends what you want to do.
Try this,
You have to create a simple module for your requirement, means what you are trying to achieve with included php file.
then place that module within the article with {loadposition module_position}
then you will get all the POST variables to that article and also suppose user_id and all other joomla Factory can be accessed bcoz its a Joomla module.
for a quick tutorial about module creation can be found here.
hope it make sense.
I resolved this using an Extension called Sourcerer
https://www.nonumber.nl/extensions/sourcerer
Variables can be passed without issue now.
Thanks for the input.
Why do not you try to create a new Joomla component and do whatever you want it it?
I am absolutely beginner in php/mysql but managed to do it with my page, following this instructions:
http://www.inmotionhosting.com/support/edu/joomla-3/create-component/helloworld
When you create this component, whatever you write in its "default.php" file, is actually a blank HTML/PHP page, with your joomla design and some other Joomla-features.
Sorry for the amateur answer and terminology :)

Joomla output override from plugin

Is there a way to override a component's output from a plugin? I'm aware of the concept of template overrides, but I just don't want to put the code into the template because
it simply feels wrong to put the code there because it is not a feature tied to the template. In my opinion the template should only contain overrides to fit core output into the template
I may want to add parameters to change behaviour (i.e. there is business logic involved and I want to keep it all together - in a plugin)
I may reuse it at another webpage and obviously don't want to copy & paste the code to all the templates I use
Or are my considerations wrong and the answer is: Just keep it simple and put it into the template?
I found a tutorial about Adding custom fields to core components using a plugin in the joomla documentation where the costom fields are addet to the view with a template override - which seems odd because the rest is done in a plugin - so i assume there is no way to to an output override from a plugin?

Datalife engine - where are the functions in .tpl files called from?

I'm migrating my blog from wordpress to datalife engine. For what i figured it out by searching, is writen in smarty php... I never worked with it before and i want to query the database to implement something on the theme... But i can't write it on the tpl files...
What i want to know is, where are functions like {rating}, {content} or [related-news] declared...
i want to query the database, but for what i know, i can't do it on the tpl files direcly.
And i can't find any info where they are because all the readme files of datalife are in russian. Can someone help me to figure out how is this structure done?
Datalife Engine use the module files in folder /engine/modules/ to define which .tpl files to use for which function, so there is no single file that declare all tpl files, you have to check which module or which function you want to edit, in your case it seems like you are looking for full page of content which in DLE called "fullstory" the module that handle this module is engine/modules/show.full.php and the theme file to use for this module are fullstory.tpl to dispaly the rest of fullstory, addcomment.tpl for add comment form and relatednews.tpl to display related news, about {rating} there are at least 3 modules use this tag which are show.short.php, show.custom.php and show.full.php, however the DLE structure is each module use its own tpl file to display as {content}, I think if you go to dlestarter.com you can get all answer you need from them, I think they are not so advanced team but they do speak English so they can help you more effectively to any questions you may have...good luck!
Datalife Engine does not use smarty template engine!.
It has it's on custom engine.
The Flow of the cms its simple, every article has a short story and optionally a full story.
In the template folder you have a bunch of tpl files with build up the final page.
main.tpl beeing the file called for every page you display.
Now typically once you call the front page the main.tpl along with shortstory.tpl are called along with other blocks for the front page.
the shortstory.tpl is build by the /engine/modules/show.short.php file witch will call the db for the data or memcached if enabled and the data was cached replace the the engin tags with the necessary data like {title} and many others.
You should really look at the documentation to undestand witch tags are found in witch section.
One important think to note is that you cannot add php in the tpl files at least not out of the box but you can include files with php in them.
I recommend heading over to dlestarter.com they are a good reference for documentation in English and pretty much the only once as well. They also have a forum where i look into sometimes.

Can I disable PHP within a Laravel (Blade) view file?

I was to understand that MVC templating was used as a means of locking down a view from using any substantial programming logic. Testing the Blade system for Laravel 4, I notice that I am still able to include PHP content into the view.
Can I disable PHP in a Blade template? Essentially turning the file into a .html file with some additional possibilities (eg, Blade looping and sections/includes).
Blade templates compile to php, so you won't be able to eliminate php altogether.
There is something you can work around your project by creating your own compiler, or extending Blade's.
You will need to work your own solution out on how to handle rendering the parts in php that are used in your template, I would just render them as is, for example:
- if someone forgot an <?php echo $example; ?> that's what the engine would render.
If you need programming logic in your views you're probably doing something wrong. Try to do the logic in your controller and inject the variables into your views.

Categories