4 Replies Latest reply on Mar 1, 2007 12:06 PM by Newsgroup_User

    Adding sections for INI in application code

    cutie369 Level 1
      Hi guys, I got this nice code from Ray Camden's blog last night and it initiates your ini variables and keys into the application scope. The example only shows how to add on section from the INI. How do I add multiple sections from the INI? For Example, I need to add a section called grid.

      <cfif not isDefined("application.settings") or isDefined("url.reinit")>
      <cfset iniFile = expandPath("./config.cfm")>
      <cfset sections = getProfileSections(iniFile)>
      <cfset data = structNew()>
      <cfif structKeyExists(sections, "default")>
      <cfloop index="key" list="#sections.default#">
      <cfset data[key] = getProfileString(iniFile, "default", key)>
      </cfloop>
      <cfset application.settings = data>
      <cfelse>
      <cfthrow message="Ini file has a missing default section!">
      </cfif>
      </cfif>
        • 1. Re: Adding sections for INI in application code
          cutie369 Level 1
          I know there has to be a gentleman who can please help me with this.
          • 2. Re: Adding sections for INI in application code
            Level 7
            I would guess something like this. Of course I would probably want to
            rework those whole process so that the sections are dynamic.

            <cfif structKeyExists(sections, "grid")>
            <cfloop index="key" list="#sections.grid#">
            <cfset data[key] = getProfileString(iniFile, "grid", key)>
            </cfloop>
            <cfset application.settings = data>
            <cfelse>
            <cfthrow message="Ini file has a missing grid section!">
            </cfif>
            • 3. Re: Adding sections for INI in application code
              cutie369 Level 1
              Hi Ian, I want the default section also. So, are you saying to do something like this? Is there no way I can combine this.

              <cfif not isDefined("application.settings") or isDefined("url.reinit")>
              <cfset iniFile = expandPath("./config.cfm")>
              <cfset sections = getProfileSections(iniFile)>
              <cfset data = structNew()>
              <cfif structKeyExists(sections, "default")>
              <cfloop index="key" list="#sections.default#">
              <cfset data[key] = getProfileString(iniFile, "default", key)>
              </cfloop>
              <cfset application.settings = data>
              <cfelse>
              <cfthrow message="Ini file has a missing default section!">
              </cfif>
              <cfif structKeyExists(sections, "grid")>
              <cfloop index="key" list="#sections.grid#">
              <cfset data[key] = getProfileString(iniFile, "grid", key)>
              </cfloop>
              <cfset application.settings = data>
              <cfelse>
              <cfthrow message="Ini file has a missing grid section!">
              </cfif>
              </cfif>
              • 4. Re: Adding sections for INI in application code
                Level 7
                That is the roughest way to do this. Add an if block for each section.
                But yes, there should be a way to combine this, but exactly how
                depends greatly on your requirements.

                A fairly simple, but limited way would be create a list of the sections
                and loop over that list.

                <cfset sectionList = "default,grid,...")>

                <cfloop list="#sectionList#" index="section">
                <!--- if block here using #section# everywhere a section title is
                expected --->
                </cfloop>

                I would also think it would not be too difficult to parse the INI file
                for the sections itself, but that would take detailed knowledge of the
                INI file structure so I can not easily advise you on that.