I'm trying to set up my website so changing one config value reflects in a site-wide change. The most obvious example is changing the site title.
I tried using config('constants.site_title') or something like that but it doesn't look like it can be accessed on the twig without initializing it in the php code section.
I tried using plugin's Settings as outlined in https://octobercms.com/docs/plugin/settings#backend-pages but that also was hard to access from the twig views.
Finally I found that using theme custom form data works (see https://octobercms.com/docs/themes/development#customization). Is this the right way to do it? I feel like this use case must have been encountered before and there must be a best practice for it.
Is this the right way to do it?
I think its a reasonable choice - supports most of field types including repeater, has its own "Configure customization options for the active theme" permission, and is easily accessible everywhere in Twig with {{ this.theme.<field_name> }}
Related
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.
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 :)
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?
I am working on one concrete5 site and need to add text using placeholder in concrete5's default search block.
Edited the code like this:
<input name="query" type="text" value="<?php echo htmlentities($query, ENT_COMPAT, APP_CHARSET)?>" class="ccm-search-block-text" placeholder="Search"/>
file path is : siteroot\concrete\blocks\search\view.php.
When I use the same stuff in local Apache server, its working. But not working with other server.
Any ideas or suggestions? Thanks.
My guess is that you either changed code in the wrong part of the template (which is easy to do because the markup for the built-in blocks is extremely messy and not well-thought-out), or you have another file that is overriding the base view.
First of all, you should not edit the built-in view.php file -- instead you should override it by copying the file to SITEROOT/blocks/search/view.php. Now in that new copy of the file, make the changes you want to.
Next, try making some other changes and see if those come through -- for example, try just adding some random text to the file and see if that gets displayed. The search block combines a lot of different functionality into one place, so it's possible you're changing the file in one spot but that spot is never actually rendered (for example, the search block outputs both the search form and the search results when the form is submitted).
Also you should disable the Overrides Cache in the dashboard -- this causes a lot of problems of this nature during development.
There isn't anything in the search block itself that would change your markup, so it's probably due to caching or not having your code in the place that you think it's in.
Good luck.
Here is an idea.. Rather than trying to add to the php outside of Concrete, why not try this? I have used it a number of times with our sites that I have put together..
https://www.concrete5.org/marketplace/addons/simple-php-block/
HTH,
Kent
Curious if anyone has tried something like this/if it would work:
Many premium WordPress themes allow the administrator to change theme setting like color and typography and then (typically) either inject CSS directly into the header to override the defaults OR switch between a number of pre-set stylesheets.
Would it be possible to allow an administrator to instead use theme options to set base colors, typography rules, etc. and then use these to set LESS/SASS variables and run the compiler to spit out a completely new style.css file that would be rebuilt using those variables in the place of the default values (obviously we would also want a way to revert to the default).
I THINK this should be possible (and might actually be a great lightweight approach to allow for some pretty advanced customization) but I'm curious if anyone has tried something similar and what potential drawbacks I might run into.
This is certainly possible, though I have never done it personally. I would start out with a PHP less compiler like LESS PHP and use that both to compile the theme stylesheet and to re-compile based on options set in a WordPress admin menu.