Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2018-10-05 19:36:10

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

How can I get this form's active theme name in txp:php?

I have a page where I need to publish custom CSS in the head, instead of having it linked from an external file.

Prior to Textpattern 4.7, I was using rvm_css to write the custom CSS to files, and I had the following snippet in my template to pull in the CSS:

  <txp:if_custom_field name="custom_css">

        <txp:variable name="article_stylesheet_name"><txp:custom_field name="custom_css"/>.css</txp:variable>

      </txp:if_custom_field>

      <style>
<![CDATA[

        <txp:if_custom_field name="custom_css">

          <txp:php>
            global $variable;
            echo(file_get_contents('./css/' . strtolower($variable['article_stylesheet_name'])));
          </txp:php>

        </txp:if_custom_field>

]]>
      </style>

With Txp 4.7+, rvm_css adds the active theme name to the path, as a directory holding CSS assets. That makes sense, but I don’t know how to access that variable in my PHP.

Can anyone shed some light on this?

Thank you!

Offline

#2 2018-10-05 21:13:16

jakob
Admin
From: Germany
Registered: 2005-01-20
Posts: 4,599
Website

Re: How can I get this form's active theme name in txp:php?

Perhaps page_url and type="theme_path" or type="themes_root" can help you. You should be able to do:

<txp:page_url type="theme_path" />/styles/<txp:custom_field name="custom_css"/>.css

or:

<txp:page_url type="themes_root" />/your-theme/styles/<txp:custom_field name="custom_css"/>.css

TXP Builders – finely-crafted code, design and txp

Offline

#3 2018-10-08 20:53:45

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: How can I get this form's active theme name in txp:php?

Thank you, @jakob!

Unfortunately, the path that this spits out isn’t compatible with my use-case.

The main thing is that PHP’s file_get_contents() function needs a path to the file on the disk, but the theme_path and themes_root tags (understandably) output URLs. Being part of the page_url tag, it makes perfect sense that they give you URLs.

I don’t want to ditch rvm_css because it lets the site editor create new static CSS files without messing around (and possibly messing up) the site theme. And rvm_css outputs CSS files to a directory named after the theme under the css directory you designate in preferences, not the theme directory itself.

For example, my theme directory looks is like this:

/document_root/themes/mysite.tld/*

But the rvm_css files live in a directory path like this:

/document_root/css/mysitetld/*

(Note that rvm_css strips out the dot from my theme name.)

It looks like, for now at least, I need to hard code the theme name in the template.

I thought Textpattern might have some way to expose the current theme name used by rvm_css so I can tell file_get_contents() where my CSS is on the disk.

But I guess that’s not the case right now.

Thank you again!

Last edited by johnstephens (2018-10-08 20:54:05)

Offline

#4 2018-10-08 21:13:58

etc
Developer
Registered: 2010-11-11
Posts: 5,057
Website GitHub

Re: How can I get this form's active theme name in txp:php?

Looking at rvm_css code, it seems that the path is

global $pretext;

$csspath = get_pref('path_to_site').DS.
    get_pref('rvm_css_dir').DS.
    strtolower(sanitizeForUrl($pretext['skin'])).DS.
    strtolower($variable['article_stylesheet_name']);

This said, importing a flat file content into txp rather than linking to it is questionable.

Offline

#5 2018-10-08 22:44:40

johnstephens
Plugin Author
From: Woodbridge, VA
Registered: 2008-06-01
Posts: 999
Website

Re: How can I get this form's active theme name in txp:php?

Thank you, @etc! I should have thought to check the source of rvm_css.

This said, importing a flat file content into txp rather than linking to it is questionable.

This template is for generating ebooks via an external script. The script will use CSS that is rendered in the document, but won’t fetch external CSS files.

Offline

#6 2018-10-09 07:15:29

Bloke
Developer
From: Leeds, UK
Registered: 2006-01-29
Posts: 11,273
Website GitHub

Re: How can I get this form's active theme name in txp:php?

If file_get_contents() isn’t letting you use urls then it may be due to a security setting in your hosting environment. PHP can let you use the two systems (path or URL) interchangably to fetch content unless prohibited.

That reminds me: check smd_ebook for 4.7.x compatibility.


The smd plugin menagerie — for when you need one more gribble of power from Textpattern. Bleeding-edge code available on GitHub.

Txp Builders – finely-crafted code, design and Txp

Offline

#7 2018-10-09 09:12:58

etc
Developer
Registered: 2010-11-11
Posts: 5,057
Website GitHub

Re: How can I get this form's active theme name in txp:php?

johnstephens wrote #314483:

This template is for generating ebooks via an external script. The script will use CSS that is rendered in the document, but won’t fetch external CSS files.

Makes sense, thanks. But then you could also import CSS from the db, I don’t think there is any difference performance-wise. We could even enable CSS output in 4.7.2 via, say, <txp:css format="inline" />.

Offline

Board footer

Powered by FluxBB