• Hi,
    anyone have a good solution for this please? it might be only fixable at plugin level
    https://github.com/eliot-akira/custom-content-shortcode/issues/12

    shortcode outputs extra whitespace for every line.

    It would be useful if extra line breaks/double space etc between shortcode tags was removed from output

    if i have a CCS like this:

    
    [if check={COURSE_TYPE} value="new"][set type]new[/set][else][set type][/set][/if]
    £[format decimals=0 point=. thousands=,]
    [if check={TYPE} value="new"]
    [global][data key="course-information-new" field="total_fees"][/global]
    [else]
    [global][data key="course-information" field="total_fees"][/global]
    [/if]
    [/format]
    [/pass]
    

    all of those lines create whitespace around the output

    normally it’s not a problem but when run through an HTML compressor on a line like this it’ll end up stripping white space between words because of the extra whitespace from the shortcode (double spaces etc)

    so…

    New Courses: [my_shortcode] Fees

    will come out something like this after compression

    New Courses:£1,000Fees

    since the uncompressed HTML is actually something like

    
    New Courses
    
    £1,000
    
    Fees
    

    essentially I’d have to write it like this…
    [if check={COURSE_TYPE} value="new"][set type]new[/set][else][set type][/set][/if]£[format decimals=0 point=. thousands=,][if check={TYPE} value="new"][global][data key="course-information-new" field="total_fees"][/global][else][global][data key="course-information" field="total_fees"][/global][/if][/format][/pass]
    …which is hard to read

    thanks
    J

Viewing 10 replies - 1 through 10 (of 10 total)
  • Will using the [raw] .. [/raw] shortcodes around your code help?

    Thread Starter codemonkeynorth

    (@codemonkeynorth)

    Hi @iantresman

    No I tried that and it didn’t help

    Again, if I don’t compress the html it’s fine because the browser ignores the whitespace.

    However the html compressor (Fast Velocity Minify) strips the extra. I don’t think it can be normal spaces as it’d presumably only convert 1+ spaces to 1 space. Therefore it must be the line breaks between the [format] [if] etc

    (Well more specifically a space followed by a line break seemingly will get removed entirely, since there’s no real reason to have a space before a line break. It’s not a fault of the compressor as such, it’s doing what it should do)

    If I’m outputting the shortcode with PHP I can run a function to clean the output up manually, it’s just I need to apply this to every possible template field that might have a CCS shortcode in it.

    Ideally therefore CCS would strip the line breaks between closing and opening [...] entries before creating it’s output. The only problem there is I guess in fact you might actually want those line breaks (auto formatting)

    I think therefore we could do with something like a [spaceless]....[/spaceless] tag or the [raw] tag should do it? I’m just not sure on the use-cases regarding the latter

    Have you tried to fill variables and use them for output.

    So you could avoid any code related spaces in your output.

    Thread Starter codemonkeynorth

    (@codemonkeynorth)

    @polarracing the line breaks come from the line breaks in the shortcode editor between the [...] tags (end of the [if] etc). I don’t see a specific way to exclude those from output by using a variable

    Seperate code and output.

    Use your [if] clauses to fill variables instead of returning the result direct.

    ….[if check={TYPE} value=”new”] [set my-var1][global][data key=”course-information-new” field=”total_fees”][/global][/set][else]….

    Then you do not have the line breaks from [if] stuff in your output.

    And if you need to format use [set my-var][format][get my-var][/format][/set] after filling your var with the raw value.

    I normally seperate the code form the output. First run the code and fill variables, the start the html section and use the always the same variable to return different content.

    As the variables are available on the hole page, you can set you code somewhere at the top of the page and use the results all over the page.

    • This reply was modified 4 years, 8 months ago by polarracing.
    • This reply was modified 4 years, 8 months ago by polarracing.
    • This reply was modified 4 years, 8 months ago by polarracing.
    Thread Starter codemonkeynorth

    (@codemonkeynorth)

    hi @polarracing

    I mentioned in my original comment that the line breaks are there for readability of the logic. compare with the end of the post where I’ve stripped all of the line breaks.

    J

    In the Divi Theme this is always an issue. It even adds in some cases <p> around [if] clauses. Thats whay I use the seperation approach.

    If you seperate the code and the output you can keep your code radable while getting the output you need.

    I even use spaces tab in my code to show seperate levels of nesting. But as the code is not in the output area, this can be stripped to whatever without affecting my output.

    • This reply was modified 4 years, 8 months ago by polarracing.
    Thread Starter codemonkeynorth

    (@codemonkeynorth)

    Hi @polarracing

    But without putting everything onto one line in the CCS editor, I’m confused how to avoid white space? (The Logic code above isn’t in my page content field, it’s in the Shortcodes editor that comes with CCS – enable Meta Shortcodes in settings.. see /wp-admin/options-general.php?page=ccs_reference&tab=meta-shortcodes)

    Then I’m either running it as $fees = do_shortcode(‘[total_fees]’); or I have the [total_fees] shortcode within content in one of my wysiwyg ACF fields and I’m outputting that field in the template

    I don’t see how the latter is relevant though, it’s the CCS shortcode that’s returning the whitespace from its editor

    Ok, I see. I missed the part with the shortcode editor.

    Not sure with [if] in the shortcode template I use [loop] inside there only. Gives me the one result I need and has no issues with empty code lines.

    Also I would check if removing the WYSIWYG from the used field may help.

    • This reply was modified 4 years, 8 months ago by polarracing.
Viewing 10 replies - 1 through 10 (of 10 total)
  • The topic ‘shortcode outputs extra whitespace – breaks html minifiers’ is closed to new replies.