2

I'm using Mura CMS 7.1, which uses ColdFusion. On a page template I have some markup and am including a template file that has code for displaying calendar events from an outside source. When there are no events, I'm currently displaying a message as such. Instead however, I'd like to hide this entire section on the page template itself. Problem is I need to pass some sort of value from the include file back to the page template so I can set inline CSS to either display block/none for this section, and I'm not sure how to do this. My page template code is:

        <section class="collegeEvents" style="display:">
            <div class="collegeEvents__container wrapper-1170MaxWidth">
                <h2 class="collegeEvents__heading">What's coming up?</h2>
                <cfinclude template="inc/homeEvents.cfm" />
            </div>
        </section>

And the calendar code is all inside of the 'homeEvents.cfm' file. I need to be able to alter that inline css 'display' property with a value that I set in 'homeEvents.cfm'. How would I go about doing this so that the value is accessible from the page template?

JesseEarley
  • 1,036
  • 9
  • 20
  • 1
    This is not possible in CF. You will need to do some logic before the block you want to hide. All this include is really doing is injecting that code into that area. If the code was directly on the page it wouldn't be able to go back and set a variable. The only option, depending on the content of that CFM, is to run some JavaScript which will change the CSS. This JS can be in the include along side the CFML to be rendered on the page. – haxtbh Jan 26 '21 at 16:36
  • OK, so since the include is being injected into the page, it will be able to modify elements on the calling page? Another option here would be to include the
    code from my code above, into the include file itself, run some logic to see if there is any events at all, and then either output the entire thing, or nothing at all.
    – JesseEarley Jan 26 '21 at 17:14
  • I went the JS route, worked perfectly. Thank you! – JesseEarley Jan 26 '21 at 17:28

3 Answers3

1

I'm not suggesting this is good practice, but you could use a style block from code inside your included cfm. eg:

<cfsavecontent variable="variables.styleBlock">
    <style>
    <cfif myLogicHere>
        .collegeEvents {display:none;}
    <cfelse>
        .collegeEvents {display:block;}
    </cfif>
    </style>
</cfsavecontent>
<cfhtmlhead text="#variables.styleBlock#" />

You could also use javascript to change the style afterwards, but with that there's more chance of a delay where the user sees the 'wrong' layout before the style is eventually applied.

Sev Roberts
  • 1,295
  • 6
  • 7
  • Wow, excellent! I never knew the `` tag existed. Perfect use case for this tag as there aren't many use cases for needing it. – user12031119 Jan 26 '21 at 21:53
0

This is a formatted comment.

I know that variables in the calling page are available in the included page. This leads me to believe that variables in the included page are available to the calling page. Here is a simple test of that theory.

CallingPage.cfm

<cfinclude IncludedPage.cfm>
<cfdump var = "#x#">

IncludedPage.cfm

<cfset x = 1>

Browse CallingPage.cfm and see what happens. If you get an error for an undefined variable, there is always to good old session scope.

Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43
0

Please see the comment from @haxtbh. I was able to accomplish the desired task using JS directly within the include.

JesseEarley
  • 1,036
  • 9
  • 20