Go to main content

Textpattern CMS support forum

You are not logged in. Register | Login | Help

#1 2010-09-23 22:27:21

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 596
Website

<title> Question

Trying for a nicer <title> setup. With this code I get an incredibly long <title> :-) Can somebody spot what’s wrong? I’m pretty new to this CMS. Thanks.


<title>
  <txp:if_section name="default">
      <txp:site_name />
  <txp:else />
      <txp:if_section name="portfolio">
          <txp:if_individual_article>
              <txp:title /> - <txp:site_name />
          </txp:if_individual_article>
      <txp:else />
          Web Design Showcase - <txp:site_name />
      </txp:if_section>
  <txp:else />
      <txp:if_section name="resources">
          <txp:if_individual_article>
              <txp:title /> - <txp:site_name />
          </txp:if_individual_article>
      <txp:else />
          Resources - <txp:site_name />
      </txp:if_section>
  <txp:else />
      <txp:if_section name="articles">
          <txp:if_individual_article>
              <txp:title /> - <txp:site_name />
          </txp:if_individual_article>
      <txp:else />
          Writing (Articles) - <txp:site_name />
      </txp:if_section>
  <txp:else />
      <txp:if_section name="about-marc">
          About Marc - <txp:site_name />
      </txp:if_section>
  <txp:else />
      <txp:if_section name="contact-marc">
          Contact Marc - <txp:site_name />
      </txp:if_section>
  <txp:else />
      <txp:if_section name="web-design">
          Web Design - <txp:site_name />
      </txp:if_section>
  <txp:else />
      <txp:if_individual_article>
          <txp:title /> - <txp:site_name />
      </txp:if_individual_article>
  </txp:if_section>
</title>

Offline

#2 2010-09-23 23:20:35

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: <title> Question

Phew ;) It can probably be done with less code, but to show you how to do it, here is your code, with – hopefully, it’s late over here ;) – correctly nested tags. See below for other examples.

<title>
  <txp:if_section name="default">
      <txp:site_name />
  <txp:else />
      <txp:if_section name="portfolio">
          <txp:if_individual_article>
              <txp:title /> - <txp:site_name />
          <txp:else />
              Web Design Showcase - <txp:site_name />
          </txp:if_individual_article>
      <txp:else />
          <txp:if_section name="resources">
              <txp:if_individual_article>
                  <txp:title /> - <txp:site_name />
              <txp:else />
                  Resources - <txp:site_name />
              </txp:if_individual_article>
          <txp:else />
              <txp:if_section name="articles">
                  <txp:if_individual_article>
                      <txp:title /> - <txp:site_name />
                  <txp:else />
                      Writing (Articles) - <txp:site_name />
                  </txp:if_individual_article>
              <txp:else />
                  <txp:if_section name="about-marc">
                      About Marc - <txp:site_name />
                  <txp:else />
                      <txp:if_section name="contact-marc">
                          Contact Marc - <txp:site_name />
                      <txp:else />
                          <txp:if_section name="web-design">
                              Web Design - <txp:site_name />
                          <txp:else />
                              <txp:if_individual_article>
                                  <txp:title /> - <txp:site_name />
                              </txp:if_individual_article>
                          </txp:if_section>
                      </txp:if_section>
                  </txp:if_section>
              </txp:if_section>
          </txp:if_section>
      </txp:if_section>
  </txp:if_section>
</title>

I think this will do the same, and it’s a little bit shorter:

<title>
      <txp:if_individual_article>
          <txp:title /> - <txp:site_name />
      <txp:else />
          <txp:if_section name="default">
              <txp:site_name />
          <txp:else />
              <txp:if_section name="portfolio">
                  Web Design Showcase - <txp:site_name />
              <txp:else />
                  <txp:if_section name="resources">
                      Resources - <txp:site_name />
                  <txp:else />
                      <txp:if_section name="articles">
                          Writing (Articles) - <txp:site_name />
                      <txp:else />
                          <txp:if_section name="about-marc">
                              About Marc - <txp:site_name />
                          <txp:else />
                              <txp:if_section name="contact-marc">
                                  Contact Marc - <txp:site_name />
                              <txp:else />
                                  <txp:if_section name="web-design">
                                      Web Design - <txp:site_name />
                                  </txp:if_section>
                              </txp:if_section>
                         </txp:if_section>
                    </txp:if_section>
               </txp:if_section>
          </txp:if_section>
      </txp:if_individual_article>
</title>

If ‘Web Design Showcase’, ‘Resources’ etcetera are your exact section titles, it could even be shorter:

<title>
    <txp:if_individual_article>
        <txp:title /> - <txp:site_name />
    <txp:else />
        <txp:if_section>
            <txp:if_section name="">
                <txp:site_name />
            <txp:else />
                <txp:section title="1" /> - <txp:site_name />
            </txp:if_section>
        </txp:if_section>
    </txp:if_individual_article>
</title>

As I said, it’s late over here, so don’t hit me if I missed a closing tag… ;)

Offline

#3 2010-09-23 23:29:12

jsoo
Plugin Author
From: NC, USA
Registered: 2004-11-15
Posts: 1,793
Website

Re: <title> Question

<txp:else /> isn’t really analogous to an if ... else in a programming language; you can only use one <txp:else /> per txp:if block. (You can nest txp:if blocks to any depth, however, each of which can include an txp:else.)

Also note that if_section’s name attribute will accept a comma-separated list of section names. So you could combine a lot of your blocks into one.

Finally, I’ll suggest either using section titles (e.g., the section with name “portfolio” is assigned the title “Web Design Showcase” in Presentation->Sections) or using variable to assign the metadata, like this.

But even if you wanted to stick to “pure” Txp, I think you could come up with something rather simpler than Els’s example (probably the first time in the history of the universe that has been said). I’d start by wrapping the whole thing in an if_individual_article block, because that’s the same for every section (you can never have an individual article in the default section).

<title>
    <txp:if_individual_article>
        <txp:title /> - <txp:site_name />
    <txp:else />
        <txp:if_section name="default">
            <txp:site_name />
        <txp:else />
            <txp:if_section name="resources">
                WebDesignShowcase - <txp:site_name/>
            <txp:else />
                <txp:if_section name="articles">
                    Writing(Articles) - <txp:site_name/>
                <txp:else />
                    ... and so on for each section
                </txp:if_section>
            </txp:if_section>
        </txp:if_section>
    </txp:if_individual_article>
</title>

Code is topiary

Offline

#4 2010-09-23 23:53:20

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 596
Website

Re: <title> Question

Els wrote:

If ‘Web Design Showcase’, ‘Resources’ etcetera are your exact section titles, it could even be shorter:

OK, that’s the one I tried. And it does what I need it to do! Thank you both for the explanations. I hope to master this txp “else” thing soon :D

Offline

#5 2010-09-24 00:45:51

els
Moderator
From: The Netherlands
Registered: 2004-06-06
Posts: 7,458

Re: <title> Question

maruchan wrote:

I hope to master this txp “else” thing soon :D

You’d better! You haven’t covered your category and search pages yet ;)

Edit: you’re lucky, you don’t seem to have category pages ;) One else less…

Last edited by els (2010-09-24 00:52:48)

Offline

#6 2010-09-24 16:43:16

maruchan
Member
From: Ukiah, California
Registered: 2010-06-12
Posts: 596
Website

Re: <title> Question

Search pages? What do I need to do there? It already says “search” for the title…seems like enough to me, unless others are doing more :-)

Edit: I’ll probably add category stuff once I get to more than three articles :D

Last edited by maruchan (2010-09-24 16:43:37)

Offline

Board footer

Powered by FluxBB