We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3677
    • 130 Posts
    HTML resource loads (PageA)--->Using TemplateA--->
    TemplateA loads a style sheet (master.css) that is a modX resource itself using an ext file include.
    --->Master.css needs to reference the value of a tv that is set on PageA, not on Master.css

    I've done this in the past but I was retrieving a tv from a resource by name. I need this to be generic such that it will load the tv based on the value set of the current page in the browser (which will change).

    This code loads the tv I need, but I need 'pagetitle' to be a variable resource not static.

    <?php
    $context = $modx->context->key;
    
    $resource = $modx->getObject('modResource', array(
    'context_key'=>$context,
    'pagetitle'=>'Geo Page - Austin, Texas'
    ));
    
    $rawValue = $resource->getTVValue('geoImage');
    
    return $rawValue;
    
    return $rawValue;

    [ed. note: bmilton last edited this post 15 years, 2 months ago.]
      • 3749
      • 24,544 Posts
      Maybe I'm misunderstanding you, but it looks like you just want the value of a TV from the current page. If that's the case, all you should need is this single line:


      return $modx->resource->getTVValue('geoImage');


      FYI, it will be a little faster if you use the ID of the TV instead of 'geoImage'.

      return $modx->resource->getTVValue(12);
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 3677
        • 130 Posts
        Hi Bob,

        I do want the tv value from the current page (in the browser), but I think that this will give me the value from the currently processed resource instead. Because Page A loads a style sheet that is actually a Modx resource. So the 2nd resource (style sheet) is being processed and this is the one that needs to pull the tv value from Page A.

        I think what happens with the above code is it tries to return the tv for the style sheet itself and not Page A.

        Thanks,
        -Brian
          • 32316
          • 387 Posts
          I had an idea - but then realized that I'm not sure what you are doing.
          TemplateA loads a style sheet (master.css) that is a modX resource itself using an ext file include.
          having trouble parsing that - maybe a bit of your code might help...
            • 3677
            • 130 Posts
            The template is loading a standard style sheet like this:
            <link rel="stylesheet" type="text/css" href= "/master.css" />


            /master.css is not an external file it's a modX resource (document). The content for that modx document looks like this:
            [[!inc? &file=`[[++assets_path]]css/css_general.css`]]


            I am bringing the external css file into the modx realm so that I can use modx tvs inside of the style sheet.

            Inside that style sheet I would like to set a background image on a div, using the tv value set on the current page.
            #geoBox {
                background: url([[getGeoImageTv]]) no-repeat 17px 13px;
                position: relative;
            }


            So I created a snippet "getGeoImageTv'' which I want to use to grab the tv "geoImage" that is set on the loaded page.
            Problem is, since this snippet is running out of the master.css style sheet, it seems to be looking for the geoImage tv on the master.css resource instead of the html page itself.

            Hope that makes sense.
              • 33968
              • 863 Posts
              The snippet is only going to be parsed on the resource it resides on, so the only practical way to get the id of the 'html page' would be to send it as a url parameter:
              <link rel="stylesheet" type="text/css" href= "/master.css?id=[[*id]]" />
               ... or send the tv value (but that might be messy) ...
              <link rel="stylesheet" type="text/css" href= "/master.css?tv=[[*geoImage]]" />
              

              Then your snippet would have access to the parameter via $_GET.

              You'll have to call the snippet uncached, and having a parameter attached means the css file won't be cached properly by the browser either. Normally when someone visits your site they download the stylesheet once and then it's already there on subsequent page loads (until they clear their browser cache). But this way they'll have to download it on every page sad


              There are some ways around this however; check out the ideas in this thread:
              http://ttyl.modx.com/thread/69703/can-template-variables-be-used-in-stylesheets#dis-post-391001 [ed. note: okyanet last edited this post 15 years, 2 months ago.]
                • 3749
                • 24,544 Posts
                It seems like you might be going the long way around and generating a lot of extra processing for each page.

                I think I would put the CSS content in a chunk, then use a snippet with getChunk() to inject it. That way, you could use standard TV tags in the CSS and they would be parsed for the current page.

                The snippet would look like this:

                <?php
                $modx->regClientCss($modx->getChunk('ChunkName'));
                return "";


                Put a tag for the snippet at the top of the <body> section of the template and you should be good to go.

                I'm not positive that content tags will be processed during getChunk(). If not, I think I'd just use a snippet that read the contents of the CSS file with file_get_contents() instead of the getChunk() call. Then, use str_replace to set the TV tag, and call regClientCSS as above.

                You could also use a placeholder tag in the chunk, get the value of the TV in the snippet, and pass the value in the properties array as a second argument to getChunk().
                  Did I help you? Buy me a beer
                  Get my Book: MODX:The Official Guide
                  MODX info for everyone: http://bobsguides.com/modx.html
                  My MODX Extras
                  Bob's Guides is now hosted at A2 MODX Hosting
                  • 3677
                  • 130 Posts
                  Lucas and Bob, Thanks for help.

                  It seems like the easy fix here is to just add the single style rule to the head of the template itself:
                  <style type="text/css">
                  #geoBox {
                      background: url([[*geoImage]]) no-repeat 17px 13px;
                      position: relative;
                  }
                  </style>


                  This gets the image tv I have set individually on each resource and seems to be working. I don't love having CSS in the page, but it works. If I had a bunch of rules to define like this I'd like it less.

                  Bob, I'm not sure I follow your suggestion...
                  First, what is the concern regarding extra processing? Is loading the style sheet as a modx resource really going to make a significant impact? I haven't noticed any loading time problems yet. It sure is convenient for skinning a site. I've added tvs to the style sheet resource that define font and bg colors and I reference these in many places inside the style sheet to generate the desired look. That works easily since the style sheet is the current resource being parsed. Once I need a tv from another resource it's more challenging.

                  I think I would put the CSS content in a chunk
                  Are you talking about just this one style rule or the whole style sheet?

                  Put a tag for the snippet at the top of the <body> section of the template
                  You want to inject CSS rules into the <body> ? Wouldn't they need to be in the <head>? I'm confused.

                  -Brian





                    • 3749
                    • 24,544 Posts
                    Quote from: bmilton at Aug 24, 2011, 10:29 AM
                    Lucas and Bob, Thanks for help.

                    It seems like the easy fix here is to just add the single style rule to the head of the template itself:
                    <style type="text/css">
                    #geoBox {
                        background: url([[*geoImage]]) no-repeat 17px 13px;
                        position: relative;
                    }
                    </style>


                    This gets the image tv I have set individually on each resource and seems to be working. I don't love having CSS in the page, but it works. If I had a bunch of rules to define like this I'd like it less.

                    That seems like a good solution for this case.


                    Bob, I'm not sure I follow your suggestion...
                    First, what is the concern regarding extra processing? Is loading the style sheet as a modx resource really going to make a significant impact? I haven't noticed any loading time problems yet. It sure is convenient for skinning a site. I've added tvs to the style sheet resource that define font and bg colors and I reference these in many places inside the style sheet to generate the desired look. That works easily since the style sheet is the current resource being parsed. Once I need a tv from another resource it's more challenging.

                    The time difference it probably trivial, but I like to cut down the steps necessary to render a page whenever possible.


                    I think I would put the CSS content in a chunk
                    Are you talking about just this one style rule or the whole style sheet?
                    Either one. Whichever is more convenient.

                    Put a tag for the snippet at the top of the <body> section of the template
                    You want to inject CSS rules into the <body> ? Wouldn't they need to be in the <head>? I'm confused.

                    regClientCSS() will inject it at the end of the <head> section regardless of where the tag is placed and the snippet returns an empty string so nothing gets places where the tag is.
                      Did I help you? Buy me a beer
                      Get my Book: MODX:The Official Guide
                      MODX info for everyone: http://bobsguides.com/modx.html
                      My MODX Extras
                      Bob's Guides is now hosted at A2 MODX Hosting
                      • 3677
                      • 130 Posts
                      Ok, regClientCSS is the part I was missing. Thanks Bob.

                      -Brian