For getting the article alias of the current/active article, I can use
<?php echo $article->alias;?>
This will work fine if I put that code inside the article itself.
However when I put this code inside a Joomla module, it echos nothing;
Not even when I insert the module inside the article itself (with Modules Anywhere from Regular Labs).
How can I make use of the Joomla variables (or this one in particular) within a module?
Many thanks!
For now I have found an alternate solution that seems to give me the desired result.
<?php $alias=basename($_SERVER['REQUEST_URI']);?>
<?php echo $alias;?>
This apparently returns the last part of an URL string (everything behind the last slash).
Related
I've started working with the PageKit CMS (which I think is based on the symfony framework) but can't figure out how to include JS files in my view.
the documentation tells me to use <?php $view->script('theme', 'theme:js/theme.js') ?>
When I adjust this to match my module name it ends up looking like this: <?php $view->script('shoutzor', 'shoutzor:js/visualizer/main.js') ?>
But when I then load the page there's no <script> tag (or anything for that matter) regarding that js file.
What am I doing wrong here? since Pagekit is a fairly new CMS there's unfortunately hardly anything to find.
Turned out I did 2 things wrong.
First off, the 1st parameter is a unique ID apparently, which I didn't realize causing it to only add the last script from the list.
After that I modified the line to resemble something like this:
<?php $view->script('shoutzor1', 'shoutzor:assets/js/visualizer/main.js'); ?>
Which included the correct asset file
I have a site with dynamic content, with only some button labels and headers in the template files. In the View folder I have multiple *.ctp files, for example profile.ctp and index.ctp. My problem is that when I run the cake console's i18n extract function, then the translateable strings from the controller.php and profile.ctp are picked up, but index.ctp is ignored.
I tried running
cake i18n extract --files .\View\ControllerName\index.ctp --ignore-model-validation --output .\Locale\ --paths . --overwrite yes --extract-core no --merge no
This however gives no results, and no *.pom files are created, unlike when I run it without the --files argument. In that case just the strings from the view are missing from the *.pom file.
The interesting thing is that in the console output it shows that the index.ctp is parsed, but still no results are given.
Output Directory: C:\Dev\app\Locale\
---------------------------------------------------------------
Processing C:\Dev\app\View\ControllerName\index.ctp...
Processing C:\Dev\app\View\ControllerName\profile.ctp...
Done.
So, my question is - does anyone have any idea on why it is so? Is there a setting to disable certain templates, or is it the default behavior to ignore index.ctp files? Or am I just doing something really wrong?
Thank you!
Edit: Solved. Don't use shorthand PHP tags just in case :)
Maybe a stupid remark, but does your index.ctp actually contain translatable strings (i.e. strings wrapped into __("sentence to translate"); function) ?
So, after lots of headwracking I decided to try something out. I copied profile.ctp content into index.ctp, then it worked. After that I just started stripping down the index.ctp until it worked. Turns out that if I used shorthand PHP tags (<? ?>) then for some reason the translation strings detection didn't work, but when I used full tags (<?php ?>) then everything was fine. Figures :)
I'm attempting to make a template file for a CMS that I'm making where the template file can contain variables like {username} as regular text that get replaced when the page gets included on the index.php page.
Example:
Index Page:
<?php include('templates/123/index.php'); ?>
templates/123/index.php page
<?php include('header.php'); ?>
Welcome {username}
<?php include('footer.php'); ?>
I've tried several methods; however, always run into problems because the page I'm trying to change the content on includes PHP code. Every method I try either 1) messes up because the opening and closing of PHP tags within the document OR 2) just echoes out the PHP code in the document. Is there any way that I can still achieve this? Maybe even with a class of some kind? I just want to be able to achieve this safely.
I will also be using this to where custom variables like {content1} get replaces with a php code that will be ioncubed that retrieves the data from database for content located in column1, same with {column2} {column3} and {column4}. I'm just trying to make the creation of templates extremely easy. (so I'd like to make the code work for that as well)
My preferred method of doing stuff like this involves starting my code with:
ob_start(function($c) {
$replacements = array(
"username"=>"Kolink",
"rank"=>"Awesome"
);
return preg_replace_callback("/{(\w+)}/",function($m) use ($replacements) {
return isset($replacements[$m[1]]) ? $replacements[$m[1]] : $m[0];
},$c);
});
Two steps I suggest
Load the result of your file "templates/123/index.php" into a variable. see this link for how to do it assign output of execution of PHP script to a variable?
use strtr() function to replace your placeholder i.e {username} with actual values
I think this will server your needs.
I'm new to PHP and want to apply a specific class to the title of my page depending on what part of the site the viewer is browsing.
For instance, I want to apply the class "blog" to the if the viewer is at domain.com/blog OR domain.com/blog/post-1 so on and so forth BUT apply the class "pics" if they're viewing domain.com/pics or domain.com/pics/gallery-1 etc etc.
I found something that could be modified to serve my needs using javascript here
but I figured seeing as I'm using PHP already, it'd make more sense to keep this sort of thing server side.
As I say, I'm new to PHP. I've experimented with some regular expressions, but to no avail.
EDIT: Sorry not to be more specific in my first post, I am using wordpress as my CMS - this is my first stackoverflow post, I trust you can forgive me :)
<?php
if (substr($_SERVER['REQUEST_URI'], 0,5)=='/pics') {
$h1class='someclass';
}
The it depends on how you're putting the class in the tag, might be like this
?><h1 class="<?php echo $h1class; ?>">...
Rather than putting a class in based on the page, I would suggest having seperate CSS files for home.css, blog.css, whateverelse.css. Of course, these would be in addition to some sort of default.css or site.css or whatever that would contain the styles used across the site.
You can then build a function/method to create the CSS calls in the HTML header. I usually have a "Page" object that builds the actual HTML page, and a "get_css" method that spits out the CSS calls.
It's hard to get more specific without knowing how you currently build pages.
Here is the solution I ended up crafting. Credit to #m.buettner for pointing me towards explode().
<h1 id="title-text" class="
<?php #returns the category as a class
$url = array();
$url = explode('/', get_permalink());
echo $url[3];
?>
mono in-case-404">
SITE
</h1>
I am trying to find a way to write pure PHP in my articles with Joomla (currently using 1.5). The closest I got so far was to use a plugin called Sourcerer (see link)
The issue is that the PHP code is actually being rendered as HTML. When I write the below code via the Sourcerer editor:
<?php echo "Hello world"; ?>
This is what is being written to the article:
<span><</span>?php echo "Hello world"; ?<span>></span>
I am afraid that at some point this is going to break my PHP if I come up with more complex code. Also this makes the code hardly readable. Finally the issue with Sourcerer is that you can only insert new code, you can't edit what you previously added.
Does anybody know a way to write pure PHP into articles?
I would like to insist on the fact that it has to be within articles. I already found a way to do it in modules or components, for instance with Jumi.
I have used Sourcerer a couple of times and it does allow one to insert PHP snippets into articles without issues. If your php is being rendered as html, it sounds like the php isn't getting parsed as php - first thing is to make sure that the sourcerer plugin is indeed enabled so it can allow your php scripts to get parsed. (check plugin enabled status here: extensions->plugin manager->sourcerer).
Also, use the sourcerer ("Insert Code") button at the bottom of your WYSIWYG editor when adding your PHP scripts - it keeps the formatting/syntax highlighting and ensures that it is escaped properly with the {source} {/source} tags.
An update, I have the same issue with joomla 3.x, sourcerer, I have tried tinymice, JCKEdidor and JCEEditor. I have now given up with this method.
To insert php code into an article I have used a php include file and all the html is echoed inside this php include file. The php file is then referenced from within the Article Editor.
Those more proficient than me with joomla will no doubt do this correctly using a module or the like.
You can try another plugin called Direct PHP. It's nice and simple to use. Just download and install it and make sure it is enable. After this start writing your php code in joomla article.
Example:
<?php
echo "Hello World!";
?>
it supports all most every PHP functions and you can filter the functions what you don't need it from its settings.
Here is the link for the download.
Download & import plugin sourcerer https://extensions.joomla.org/extension/sourcerer/
{source} [[?php echo "Hello world"; ?]] {/source}
use this. It fine for me.